In this example, i explain Laravel 7 login with Linkedin using Socialite. Also laravel 7 socialite provides api to log in with a Linkedin account. We can easily log in using a Linkedin account in the laravel project. So in this post, we will log in with Linkedin.

Larave 7 log in with the Linkedin account becomes more and more popular in the world. Every account is linked by Linkedin account and google account. Laravel 7 provides us Socialite package that helps to social authentication.


Step 1: Install Laravel 7

In this step i install fresh laravel 7 application and setup laravel 7 application. So run the following command to get laravel 7 application.

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


Step 2: Install Laravel Socialite

In this step i install laravel socialite package that provide api to connect with Linkedin account. So run the following command on your terminal ang get laravel socialite package.

composer require laravel/socialite


After install the laravel socialite package we add providers and aliases in config file, So open your config/app.php file and add the following service provider and aliase.

'providers' => [
    ....
    Laravel\Socialite\SocialiteServiceProvider::class,
],
'aliases' => [
    ....
    'Socialite' => Laravel\Socialite\Facades\Socialite::class,
],


Step 3: Create Linkedin App

In this step we require Linkedin client id and secret that way we can get other user information. so first you create Linkedin app account then you can create from here : Linkedin Developers Apps.


After create account you can copy your client id and secret.

Now you have to set your app id, secret and call back url in config file so open the first config/services.php and set id and secret following code:

return [
    ....
    'linkedin' => [
        'client_id' => 'id',
        'client_secret' => 'secret',
        'redirect' => 'http://learnl52.hd/auth/linkedin/callback'
    ],
]


Step 4: Create Auth

In this step we install laravel ui and generate auth in laravel 7 application so, let’s run following command in your terminal:


Install Laravel UI (User Interface)

composer require laravel/ui


Create Auth:

php artisan ui bootstrap --auth


NPM Install:

npm install


Run NPM:

npm run dev


Step 5: Add Database Column

In this step i create migration for add linkedin_id in your user table. So first run bellow command:

php artisan make:migration add_linkedin_id_column


Migration : add_linkedin_id_column

Schema::table('users', function ($table) {
    $table->string('linkedin_id');
});


app/Http/routes.php

Route::get('linkedin', function () {
    return view('linkedinAuth');
});
Route::get('auth/linkedin', 'Auth\AuthController@redirectToLinkedin');
Route::get('auth/linkedin/callback', 'Auth\AuthController@handleLinkedinCallback');


Step 6: Create Controller

In this step we create controller and add method of Linkedin auth that method will handle Linkedin callback url and etc.

app/Http/Controllers/Auth/AuthController.php

namespace App\Http\Controllers\Auth;


use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Socialite;
use Auth;
use Exception;


class AuthController extends Controller
{


    use AuthenticatesAndRegistersUsers, ThrottlesLogins;


    protected $redirectTo = '/';


    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }


    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }


    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }


    public function redirectToLinkedin()
    {
        return Socialite::driver('linkedin')->redirect();
    }


    public function handleLinkedinCallback()
    {
        try {
            $user = Socialite::driver('linkedin')->user();
            $create['name'] = $user->name;
            $create['email'] = $user->email;
            $create['linkedin_id'] = $user->id;
            
            $userModel = new User;
            $createdUser = $userModel->addNew($create);
            Auth::loginUsingId($createdUser->id);
            return redirect()->route('home');
        } catch (Exception $e) {
            return redirect('auth/linkedin');
        }
    }
}


resources/views/linkedinAuth.blade.php

@extends('layouts.app')
@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Login</div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">
                        {!! csrf_field() !!}


                        <div class="form-group">
                            <label class="col-md-4 control-label">E-Mail Address</label>


                            <div class="col-md-6">
                                <input type="email" class="form-control" name="email" value="{{ old('email') }}">
                            </div>
                        </div>


                        <div class="form-group">
                            <label class="col-md-4 control-label">Password</label>


                            <div class="col-md-6">
                                <input type="password" class="form-control" name="password">
                            </div>
                        </div>


                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="remember"> Remember Me
                                    </label>
                                </div>
                            </div>
                        </div>


                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">


                                <a href="{{ url('auth/linkedin') }}" class="btn btn-primary">
                                    <strong>Login With Linkedin</strong>
                                </a>


                                <button type="submit" class="btn btn-primary">
                                    Login
                                </button>


                                <a class="btn btn-link" href="{{ url('/password/reset') }}">Forgot Your Password?</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

I hope you understand Login with linkedin..