Your cart is currently empty!
How to Implement LinkedIn login in laravel
Hi guys,
According to a research report, nowadays, users’ sessions are shorter on every website. In this case, you can simplify the process of registration and login for your application. The Socialite package is the best and most used package for social login. It is used to reduce the process. In this article, we implement a LinkedIn login to your application. Are you looking for a Google or Twitter login? you can visit our social login series for your knowledge.
Step-by-step guide on implementing LinkedIn login in laravel application
- Install Socialite Package
Let’s start with the Install Socialite package on the laravel application. In the terminal, you run the following command:
composer require laravel/socialite
You need to add the below line of code inside the config/app.php file.
$providers= [
......
Laravel\Socialite\SocialiteServiceProvider::class,
......
];
$alias = [
......
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
......
];
- Configure credentials
Goto LinkedIn Developer Platform. You must create a project for getting the API Key and API Secret Key. You can see the below screenshots:-
You can add credentials to the env file and add the environment variable on config/services.php against the LinkedIn variable.
LINKEDIN_CLIENT_ID=864pqaXXXXXXXX
LINKEDIN_CLIENT_SECRET=cBBXDXXXXXXXXXXXX
LINKEDIN_CALLBACK_URL=http://127.0.0.1:8000/auth/linkedin/callback
You can add the below code to the services.php file.
'linkedin'=>[
'client_id' => env('LINKEDIN_CLIENT_ID'),
'client_secret' => env('LINKEDIN_CLIENT_SECRET'),
'redirect'=> env('LINKEDIN_CALLBACK_URL')
]
- create a column in the user table
You make a migration file that will create a column for the users’ table and run the migration command in your application.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddlinkedinIdColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function ($table) {
$table->string('provider');
$table->string('provider_id')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropColumns('provider');
Schema::dropColumns('provider_id');
}
}
You run the php artisan migrate command. Add fields to fillable property on the User.php file.
protected $fillable = [
'name',
'email',
'password',
'provider',
'provider_id'
];
- Create a Controller for social login
You can make a controller for social login. In your terminal, you run the below command:
php artisan make:controller SocialController
We are adding two methods: The redirectToLinkedin() which redirects the user to LinkedIn to authorize the site, and The handleLinkedinCallback() which handles the user’s credentials when called back from LinkedIn. We load the social driver’s social login. Here, I am loading a LinkedIn driver.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;
use Exception;
use Inertia\Inertia;
use Illuminate\Support\Facades\Route;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
class SocialController extends Controller
{
public function redirectToLinkedin()
{
return Socialite::driver('linkedin')->redirect();
}
public function handleLinkedinCallback()
{
try {
$userInfo = Socialite::driver('linkedin')->user();
$finduser = User::where('provider_id', $user->id)->first();
if($finduser){
Auth::login($finduser);
return redirect('/');
}else{
$checkUser = User::where('email', $user->email)->first();
if($checkUser) {
$checkUser->provider = 'linkedin';
$checkUser->provider_id = $userInfo->id;
$checkUser->save();
Auth::login($checkUser);
} else {
$newUser = User::create([
'name' => $userInfo->name,
'email' => $userInfo->email,
'provider'=> 'linkedin',
'provider_id'=> $userInfo->id,
'password' => encrypt('123456dummy')
]);
Auth::login($newUser);
}
return redirect('/');
}
} catch (Exception $e) {
return Inertia::render('Auth/Login', [
'canResetPassword' => Route::has('password.request'),
'status' => 'Something Went wrong!! Try later',
]);
}
}
}
- Define the route
You need to define a route for social login in the routes/web.php file.
Route::get('auth/linkedin', [SocialController::class, 'redirectToLinkedin']);
Route::get('auth/linkedin/callback', [SocialController::class, 'handleLinkedinCallback']);
- Updating the login view
Open resources/views/auth/login.blade.php. Add the below code and change the button style according to your application interface.
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<a href="{{url('/auth/linkedin')}}" class="btn btn-secondary">Login with Linkedin</a>
</div>
</div>
Finally Done ✔️
Now we have successfully integrated LinkedIn login into your application. We can check social login. Go to the application login page and click on the login with the LinkedIn button. It redirected the user to the LinkedIn authorization page. The user allows the application, and it will redirect to the dashboard page.
Now your user will easily connect to your application. Do you want to check which mail tools are best for your application? Click Here. I hope that this post (How to implement LinkedIn login in laravel) has clarified how to integrate the Socialite package in laravel and load the drivers. If you have questions, please leave a comment and I will respond as soon as possible.
Thank you for reading this article. Please share this article with your friend circle. That’s it for the day. Stay Connected!
Cheers