Latest Code Tutorials

Laravel Linkedin Login Tutorial


Laravel Linkedin Login Tutorial is the topic, we will discuss today. In this tutorial, we can use laravel latest version 5.6.15. I am going explain to you how to Login users with Linkedin and save them into your database using Laravel. Laravel provides us Socialite package that helps with social authentication.

Laravel Linkedin Login Tutorial

First, we install Laravel Project.

Step 1: Configure Laravel Project

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

Step 2: Setup a MySQL database

Instantly, configure the database in the .env file.



Next, migrate two tables provided by Laravel 5.6 Move to your terminal and hit the following command.

php artisan migrate

It will produce two tables in your database.

  1. users
  2. password_resets  

In users table, we can add one field called linkedin_id.


 public function up()
        Schema::create('users', function (Blueprint $table) {

Immediately, migrate the table by the following command.

php artisan migrate

Step 3: Create a Laravel Authentication

Laravel provides us basic authentication. Laravel 5.6 makes implementing authentication very simple.We only want to fire one command in our terminal.

php artisan make:auth

Now, if we change the routes >> web.php file then there is one added route, and to our view is configured automatically.



Step 4: Download the laravel/socialite package for laravel linkedin integration

The socialite is a package that makes building authentication with popular social networks simple. So we are using this Laravel 5.6 specific package.

composer require laravel/socialite

Find the providers in config >> app.php file and register the SocialiteServiceProvider.

'providers' => [
        // ...

Locate the aliases in config >> app.php file and register the aliases.

'aliases' => [
        // ...
        'Socialite' => Laravel\Socialite\Facades\Socialite::class,

Step 5: Create Linkedin App To Get Tokens

Go to the Linkedin’s developers portal by following URL:

Login via your Linkedin account. You will see something like this.

Laravel 5.6 Linkedin Login Tutorial


Soon you have to click on Create Application and fill form details and create a new application.  After generated application you can see the following slide:

Laravel Socialite Login Tutorial

In above image, you can see Client ID and Client Secret, which we need in our Laravel Application.

Now, grab both of them and shift to your editor.

Drive to the config  >>  services.php file and put one extra services array by the following code.


'linkedin' => [
        'client_id' => 'xxxx',
        'client_secret' => 'xxxx',
        'redirect' => 'http://localhost:8000/callback'],

Step 6: Generate a controller

Now, we need to create one controller, which handles the Linkedin Authentication routes.

php artisan make:controller SocialAuthLinkedinController

We must implement two methods in this controller, which are following.

1)redirect():  Redirect our users to the Linkedin.

2) callback():  Handle callback from Linkedin.



namespace App\Http\Controllers;

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

class SocialAuthLinkedinController extends Controller
    public function redirect()
        return Socialite::driver('linkedin')->redirect();

    public function callback()
        try {
            $linkdinUser = Socialite::driver('linkedin')->user();
            $existUser = User::where('email',$linkdinUser->email)->first();
            if($existUser) {
            else {
                $user = new User;
                $user->name = $linkdinUser->name;
                $user->email = $linkdinUser->email;
                $user->linkedin_id = $linkdinUser->id;
                $user->password = md5(rand(1,10000));
            return redirect()->to('/home');
        catch (Exception $e) {
            return 'error';

Next step is moving to the routes >> web.php file and register the routes.


Route::get('linkedin', function () {
    return view('loginlinkedin');

Route::get('/redirect', 'SocialAuthLinkedinController@redirect');
Route::get('/callback', 'SocialAuthLinkedinController@callback');

Route::get('/home', 'HomeController@index')->name('home');

Step 7: Create LinkedinLogin view file

Create a file in the resources  >>  views  >>  loginlinkedin.blade.php and set the following code in it.


<html lang="en">
    <title>Laravel Linkedin Login</title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
    <link href="" rel="stylesheet">   
    <script src=""></script>  
    <script src=""></script>  
<div class="container">
      <h2>Laravel Linkedin Login</h2><br/>
      <form method="post" action="{{ url('/login') }}">
        <div class="row">
        <div class="col-md-4"></div>        
            <div class="form-group col-md-4">
              <label for="Email">Email:</label>
              <input type="text" class="form-control" name="email">
        <div class="row">
        <div class="col-md-4"></div>        
            <div class="form-group col-md-4">
              <label for="Password">Password:</label>
              <input type="password" class="form-control" name="password">
          <div class="row">
          <div class="col-md-4"></div>          
            <div class="form-group col-md-4">
            <input type="checkbox" name="remember"> Remember Me
          <div class="row">
          <div class="col-md-4"></div>
            <div class="form-group col-md-4">
            <a href="{{ url('/redirect') }}" class="btn btn-primary">Login With Linkedin</a>
            <button type="submit" class="btn btn-success">Login</button>
        <div class="row">
          <div class="col-md-4"></div>          
            <div class="form-group col-md-4">
            <a class="btn btn-link" href="{{ url('/password/reset') }}">Forgot Your Password?</a>

Now, if you have not begun the server, then start the server and go the following URL



Login with Linkedin Laravel

Finally, Our Laravel Linkedin Login Tutorial is over. Thank You.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.