Skip to content
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Laravel 9 Socialite Linkedin Login Tutorial Example

Laravel 9 Socialite Linkedin Login Tutorial Example

Posted on February 21, 2022April 26, 2022 By XpertPhp

In this tutorial, we will tell you how to create a Linkedin Login in Laravel Framework(Laravel 9 Socialite Linkedin Login Tutorial Example).

Normally, we have seen that many websites are using Linkedin social login. but here we can easily create the Linkedin social login using the socialite package. so you can follow the below step.

Overview

Step 1: Install Laravel

Step 2: Setting Database Configuration

Step 3: Create Table using migration

Step 4: Install Package

Step 5: Add providers and aliases

Step 6: Create a Linkedin App

Step 7: Configuration of API Key

Step 8: Create Auth

Step 9: Create Route

Step 10: Update Model and Controller

Step 11: Create Blade File

Step 1: Install Laravel

We are going to install laravel 9, so first open the command prompt or terminal and go to xampp htdocs folder directory using the command prompt. after then run the below command.

1
composer create-project --prefer-dist laravel/laravel laravel8_linkedin_login

Step 2: Setting Database Configuration

After complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.

1
2
3
4
5
6
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name(laravel9_linkedin_login)
DB_USERNAME=Enter_Your_Database_Username(root)
DB_PASSWORD=Enter_Your_Database_Password(root)

Step 3: Create Table using migration

Now, we need to add linkedin_id in the user table. so first we will add the linkedin_id in the migration list after then we will run the migration command.

Here this file We have already updated in the database/migrations/create_users_table file. so you can see the below example code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
    /** * Run the migrations.
    * * @return void
    */
    public function up() {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->string('linkedin_id')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });
    }
    /** * Reverse the migrations. * * @return void */
    public function down() {
        Schema::dropIfExists('users');
    }
} ?>

Run the below command. after the changes above file.

1
php artisan migrate

Step 4: Install Package

Now, we are going to install the Socialite package using the below command.

1
composer require laravel/socialite

Step 5: Add providers and aliases

We will add below providers and aliases in the “config/app.php” file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
'providers' => [
....
Laravel\Socialite\SocialiteServiceProvider::class,
],
'aliases' => [
....
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
]

Step 6: Create Linkedin App

here in this step, we need to Linkedin client id and client secret key, and that credentials through we can create a successfully login. if you don’t have Linkedin credentials then you have to create a Linkedin app. so you can go on https://www.linkedin.com/developers/apps/new and create it.

Step 7: Configuration of Api Key

Now, we are going to configuration of api key in app/config/services.php file.

1
2
3
4
5
'linkedin' => [
    'client_id' => 'enter your client id',
    'client_secret' => 'enter your secret key',
    'redirect' => 'http://127.0.0.1:8000/callback/linkedin',
  ],

Step 8: Create Auth

Here in this step, below command using we will create laravel UI and authentication.

1
2
3
composer require laravel/ui
php artisan ui bootstrap --auth
npm install

Step 9: Create Route

Add the following route code in the “routes/web.php” file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
use App\Http\Controllers\Auth\LoginController;
 
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
    return view('linkedinLogin');
});
Route::get('auth/linkedin', [LoginController::class, 'redirectToLinkedin']);
Route::get('auth/linkedin/callback',[LoginController::class, 'handleLinkedinCallback']);
?>

Step 10: Update Model and Controller

Here in this step, we need to update the User.php model and LoginController.php. so you can see the below example code.

app/User.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable {
     use HasFactory;
     use Notifiable;
/**
     * The attributes that are mass assignable.
     *
     * @var array
     */
protected $fillable = [ 'name', 'email', 'password', 'linkedin_id' ];
/**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
protected $hidden = [ 'password', 'remember_token', ];
/**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
protected $casts = [ 'email_verified_at' => 'datetime', ];
}
?>

app/Http/Controllers/Auth/LoginController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Socialite;
use Auth;
use Exception;
use App\Models\User;
class LoginController extends Controller {
/*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */
    use AuthenticatesUsers;
    /*** Where to redirect users after login.
** @var string
*/
    protected $redirectTo = '/home';
    /*** Create a new controller instance.
* * @return void
*/
    public function __construct() {
        $this->middleware('guest')->except('logout');
    }
    public function redirectToLinkedin() {
        return Socialite::driver('linkedin')->redirect();
    }
    public function handleLinkedinCallback() {
        try {
            $user = Socialite::driver('linkedin')->user();
            $finduser = User::where('linkedin_id', $user->id)->first();
            if ($finduser) {
                Auth::login($finduser);
                return redirect('/home');
            } else {
                $newUser = User::create(['name' => $user->name, 'email' => $user->email, 'linkedin_id' => $user->id]);
                Auth::login($newUser);
                return redirect()->back();
            }
        }
        catch(Exception $e) {
            return redirect('auth/linkedin');
        }
    }
} ?>

Step 11: Create Blade File

Finally, We will create a linkedinLogin.blade.php file in the “resources/views/” folder directory and paste the below code.
linkedinLogin.blade.php

1
2
3
4
5
6
7
8
9
<div class="container">
<div class="row">
<div class="col-md-12 row-block">
<a class="btn btn-lg btn-primary btn-block" href="{{ url('auth/linkedin') }}">
<strong>Login With Linkedin</strong>
</a>
</div>
</div>
</div>

Please follow and like us:
error
fb-share-icon
Tweet
fb-share-icon

Recommended Posts:

  • Laravel 9 Fullcalendar Example Tutorial
  • Laravel 8 Newsletter Tutorial With Example
  • Laravel 8 Pagination Example Tutorial
  • Laravel 7 MongoDB CRUD Tutorial Example
  • Laravel 8 Pdf Generator Tutorial Using Dompdf
Laravel Tags:laravel 9 example, laravel 9 tutorial

Post navigation

Previous Post: Laravel 9 Instamojo Payment Gateway Integration Example Tutorial
Next Post: Laravel 9 Socialite Twitter Login Tutorial Example

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tricks jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch Laravel Socialite laravel social login nodejs pagination payment gateway php with mysql react js tutorial rewrite rule send mail validation wysiwyg editor

Latest Posts

  • How to Convert Date and Time from one timezone to another in php
  • how to get current date and time in php
  • Drag and Drop Reorder Items with jQuery, PHP & MySQL
  • Laravel 9 Toastr Notifications Example Tutorial
  • Laravel 9 CRUD Operation Example Using Google Firebase
  • Laravel 9 CKeditor Image Upload With Example
  • Laravel 9 Summernote Image Upload With Example
  • Laravel 9 Stripe Payment Gateway Integrate Example
  • How To Send Email Using Mailtrap In Laravel 9
  • Laravel 9 Fullcalendar Ajax Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Age Calculator Online
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Copyright © 2018 - 2022,

All Rights Reserved Powered by XpertPhp.com