In this tutorial you will learn about the Laravel 8 Socialite Login with Linkedin Tutorial Example and its application with practical example.
In this Laravel 8 Socialite Login with Linkedin Tutorial Example I’ll show you how to create LinkedIn login in laravel 8 using socialite. In this tutorial you will learn to integrate LinkedIn login with socialite in laravel. In this article we will create login with linkedin using socialite in laravel 8 application. In this example I will also guide you through the process to integrate login with linkedin using socialite in laravel.
Laravel Socialite Login with LinkedIn
As we all know that users are not much interested in filling up long registration form to register with any application. Allowing users to login with their social media accounts is quick and powerful way to get registered/verified users for your laravel application. Allowing users to login with their social media accounts makes registration/login process much easier, it also encourages more users to register for your application.
Laravel 8 Socialite Login with Linkedin Tutorial Example
In this step by step tutorial I will demonstrate you how to integrate linkedin login with socialite in laravel application. Please follow the instruction given below:
- Step 1: Set Up Laravel Project
- Step 2: Make Database Connection
- Step 3: Install Jetstream Library
- Step 4: Configure Socialite Pacakage
- Step 5: Add and Migrate Linkedin Property in Users Table
- Step 6: Add Linkedin Client ID and Secret
- Step 7: Prepare Controller
- Step 8: Define Routes
- Step 9: Set Up Login View
- Step 10: Start Laravel App
Set Up Laravel Project
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project laravel/laravel --prefer-dist laravel-linkedin-login-example |
Make Database Connection
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=db_name DB_USERNAME=root DB_PASSWORD= |
Install Jetstream Library
In this step, install jetstream laravel auth scaffolding package with livewire. Please follow the instruction.
1 |
composer require laravel/jetstream |
Now run command to generate ready made auth templates:
1 |
php artisan jetstream:install livewire |
Next, use command to install required npm packages:
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, ], .... .... |
Add and Migrate Linkedin Property in Users Table
In this step we will create a migration file. Please run the following command:
1 |
php artisan make:migration add_social_auth_id_field |
Thereafter add the new table values in the database/migration/add_social_auth_id_field.php file:
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 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class AddSocialAuthIdField extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('users', function ($table) { $table->string('oauth_id')->nullable(); $table->string('oauth_type')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('users', function ($table) { $table->dropColumn('oauth_id'); $table->dropColumn('oauth_type'); }); } } |
Next, open app/Models/User.php file and add new social auth fields within the file:
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 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<?php namespace App\Models; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Fortify\TwoFactorAuthenticatable; use Laravel\Jetstream\HasProfilePhoto; use Laravel\Sanctum\HasApiTokens; class User extends Authenticatable { use HasApiTokens; use HasFactory; use HasProfilePhoto; use Notifiable; use TwoFactorAuthenticatable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', 'oauth_id', 'oauth_type' ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', 'two_factor_recovery_codes', 'two_factor_secret', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; /** * The accessors to append to the model's array form. * * @var array */ protected $appends = [ 'profile_photo_url', ]; } |
Finally, execute command to migrate new values into the user tabel: