Login with LinkedIn in Laravel Using Laravel Socialite

Adding social login system on the website is the trend nowadays. By integrating social login, it will skip the verification process of a user. Social sites have already verified users so our application doesn’t need to follow the same process with a user. We can rely on social sites and allow the user to use our system if they log in through a social site on our website. Facebook, Twitter, Google, LinkedIn are the popular social sites that are normally integrated as a social login to the website. In this article, we will cover a login with LinkedIn in Laravel.

For our tutorial, I will integrate LinkedIn login on the fresh Laravel installation. I am going to install Laravel, create a ‘users’ table, and install the packages required to integrate the social login system.

Run the command below to install Laravel. Here I am creating a project as ‘linkedinlogin’.

composer create-project --prefer-dist laravel/laravel linkedinlogin

After installation, create the database and set the credentials in the .env file. We are building a login system so we should store user details in the database.

This tutorial is based on LinkedIn sign in so we will get the user’s LinkedIn profile ID after successful authentication. To store this social id we are adding columns provider and provider_id. Here provider column will store the value ‘linkedin’ and provider_id would store the user’s social id.

When we used social login we don’t need to store passwords so the password column value should accept ‘NULL’ as default. There is no guarantee of getting the user’s email after social login so for curiosity we need to set the email column also to accept ‘NULL’.

Open the users table migration file and add the code below.

 * Run the migrations.
 * @return void
public function up()
    Schema::create('users', function (Blueprint $table) {

Run the command below which will create the tables in our database.

php artisan migrate

We also need to add these two columns in our user model.


 * The attributes that are mass assignable.
 * @var array
protected $fillable = [
    'name', 'email', 'password', 'provider', 'provider_id'

Create LinkedIn Application

In order to integrate LinkedIn login, you should create the application on your LinkedIn account. We need client id, client secret of LinkedIn application. We also need to set an authorized redirect URL in the application.

  • Go to LinkedIn Developer Network.
  • Click on the ‘Create Application’ button.
  • Complete the information on the form.
  • Add http://localhost:8000/linkedin/callback in the Authorized Redirect URLs field.
  • Copy the Client ID and Client Secret keys.

After the above steps, you need to add the product ‘Sign in With LinkedIn’ to your LinkedIn application. Click on the ‘products’ tab and choose the ‘Sign In with LinkedIn’. Upon selecting this product, it will go for review and then included as an added product. This may take some time for review. In my case, it took around 10 minutes.

Create Routes and Controller

We can now start adding a code in Laravel. Create a controller LoginController using the command:

php artisan make:controller LoginController

Next, create a blade file login.blade.php and add the following HTML to it.

<a href="{{ url('/login/linkedin') }}">
    {{ __('Login with LinkedIn') }}

Call this view from the index method of LoginController as follows.

public function index()
    return view('login');

Let’s define the routes for it. I am also adding a few more routes which will be required in the next part of the tutorial.

Route::get('login', 'LoginController@index');
Route::get('login/{provider}', 'LoginController@redirectToProvider');
Route::get('{provider}/callback', 'LoginController@handleProviderCallback');
Route::get('/home', function () {
    return 'User is logged in';

When a user will login with LinkedIn, I will redirect them to the home route. It’s just for demo purposes. You should change this route to something else.

Login with LinkedIn Laravel

Laravel has an official package Socialite. This package helps us to easily integrate the social login system in Laravel. Install this package using the command:

composer require laravel/socialite

After installing the library, open config/services.php and add LinkedIn configuration as follows:

'linkedin' => [
    'client_id' => env('LINKEDIN_CLIENT_ID'),
    'client_secret' => env('LINKEDIN_CLIENT_SECRET'),
    'redirect' => env('LINKEDIN_CALLBACK_URL'),

Now, we need to add our constant values in the .env file.


Make sure to replace placeholders with their actual values. The final adjustments should go in the LoginController.php file. In this file, we are writing a function that redirects a user to the LinkedIn login page, and on successful authorization, it will log the user in.



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Socialite;
use App\Models\User;
use Auth;

class LoginController extends Controller
     * Where to redirect users after login.
     * @var string
    protected $redirectTo = '/home';

     * Call the view
    public function index()
        return view('login');

     * Redirect the user to the Linkedin authentication page.
     * @return Response
    public function redirectToProvider($provider)
        return Socialite::driver($provider)->scopes(['r_liteprofile', 'r_emailaddress'])->redirect();
     * Obtain the user information from Linkedin.
     * @return Response
    public function handleProviderCallback($provider)
        $user = Socialite::driver($provider)->user();
        $authUser = $this->findOrCreateUser($user, $provider);
        Auth::login($authUser, true);
        return redirect($this->redirectTo);
     * If a user has registered before using social auth, return the user
     * else, create a new user object.
     * @param  $user Socialite user object
     * @param $provider Social auth provider
     * @return  User
    public function findOrCreateUser($user, $provider)
        $authUser = User::where('provider_id', $user->id)->first();
        if ($authUser) {
            return $authUser;
        return User::create([
            'name'     => $user->name,
            'email'    => $user->email,
            'provider' => $provider,
            'provider_id' => $user->id

In our LoginController, we set the scopes ['r_liteprofile', 'r_emailaddress'] which are required to fetch basic information of a LinkedIn user. In the callback method, we are checking if the user exists and if it is not then add the user to the database.

I hope you understand how to Login with LinkedIn in Laravel. I would like to hear your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

1 thought on “Login with LinkedIn in Laravel Using Laravel Socialite

Leave a Reply

Your email address will not be published. Required fields are marked *