How to integrate LINKEDIN LOGIN in Laravel 10

Spread the love

Hello guys,

When developing a integrate LINKEDIN LOGIN with Laravel application with integrate LINKEDIN LOGIN. You must incorporate social logins like LinkedIn, Facebook, or Google Login into that. How will you respond? Laravel was used to create this socialite package. Using this package, you may add or include various social login buttons in your Laravel application.

Therefore, using the Socialite package, you will learn how to integrate a social LinkedIn login into a Laravel 10 application in this article.

How to integrate LINKEDIN LOGIN in Laravel 10 example:

You must have a LinkedIn secret id and secret key before integrating the LinkedIn login feature in Laravel 10. If you don’t already have it, get the LinkedIn secret key ID by following the instructions below.

Step 1:- Create LinkedIn app

By following this URL create a LinkedIn app:-

https://www.linkedin.com/developers/apps/new

The page below will be displayed when you click the URL provided above. So enter the information and make your LinkedIn app:

Also Read: How to remove a column from a table in Laravel migration?

image 1 How to integrate LINKEDIN LOGIN in Laravel 10

Step 2:- Set the redirect URL

After creating the LinkedIn console app. The redirect URL must then be established in OAuth 2.0 settings. for example:

image 2 How to integrate LINKEDIN LOGIN in Laravel 10

Step 3:- Copy the client id and secret

Finally, linkedin.com redirects you to the dashboard. As seen in the following image, you can copy the client ID and secret from the LinkedIn app dashboard:

image 3 How to integrate LINKEDIN LOGIN in Laravel 10

The LinkedIn app has now been successfully developed. You can now begin integrating LinkedIn login with Laravel 10 applications.

Step 4:- Setup Linkedin Client id, Secret, and URL

Configure the Linkedin app using this Laravel app in this stage. So, open any text editor and the Laravel project for Linkedin social login. The client id, secret, and callback URL are then added by opening the service.php file in the config directory and navigate there:

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

Step 5:- Install Socialite Package

Once your Laravel application’s database has been installed and configured.

Then, in order to install the Socialite package in Laravel 10 applications, you must type the following command at a terminal or command prompt:

Also Read: How to Remove Public From URL in Laravel 10

composer require laravel/socialite

Configure this package after that in the app.php file. Open the app.php file in your Laravel application configuration directory. After that, incorporate the following providers and aliases into it:

'providers' => [
    /*
     * Package Service Providers...
     */
    Laravel\Socialite\SocialiteServiceProvider::class,
]
 
'aliases' => [
    ...
    'Socialite' => Laravel\Socialite\Facades\Socialite::class,
]

Step 6:- Add Social Login Field In Table Using Migration

To create a migration file, use the following command on the terminal or command prompt in this step:

php artisan make:migration add_social_login_field_users_table

After that, open the database/migration directory’s add_social_login_field_users_table.php file and insert the following code there:

<?php
   
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
    
class AddSoicalLoginField extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function ($table) {
            $table->string('social_id')->nullable();
            $table->string('social_type')->nullable();
        });
    }
    
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function ($table) {
            $table->dropColumn('social_id');
           $table->dropColumn('social_type');
         });
    }
}

after a database table’s field has been successfully added. Then, in the User.php model, located in the app/Models/ directory, add a fillable attribute as follows:

<?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',
        'social_id',
        'social_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',
    ];
}

Step 7:- Add Login Routes

You must define the following routes in your web.php file at this stage.

To view the web.php file, go to the routes directory. Add the following routes to the web.php file after that:

Also Read: How to Integrate Stripe Payment Gateway In Laravel 10

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LinkedinSocialiteController;
 
 
Route::get('auth/linkedin', [LinkedinSocialiteController::class, 'redirectToLinkedin']);
Route::get('callback/linkedin', [LinkedinSocialiteController::class, 'handleCallback']);

Step 8:- Create Linkedin Login Controller

php artisan make:controller LinkedinSocialiteController

After that, open the LinkedinSocialiteController.php file in any text editor by going to the app/http/controllers directory. In the LinkedinSocialiteController.php file, add the following code:

<?php
   
namespace App\Http\Controllers\Auth;
   
use App\Http\Controllers\Controller;
use Socialite;
use Auth;
use Exception;
use App\Models\User;
   
class LinkedinSocialiteController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function redirectToLinkedin()
    {
        return Socialite::driver('linkedin')->redirect();
    }
       
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function handleCallback()
    {
        try {
     
            $user = Socialite::driver('linkedin')->user();
      
            $finduser = User::where('social_id', $user->id)->first();
      
            if($finduser){
      
                Auth::login($finduser);
     
                return redirect('/home');
      
            }else{
                $newUser = User::create([
                    'name' => $user->name,
                    'email' => $user->email,
                    'social_id'=> $user->id,
                    'social_type'=> 'linkedin',
                    'password' => encrypt('my-linkedin')
                ]);
     
                Auth::login($newUser);
      
                return redirect('/home');
            }
     
        } catch (Exception $e) {
            dd($e->getMessage());
        }
    }
}

Now open the browser and hit the following URLs on it:

http://127.0.0.1:8000/auth/linkedin

We trust you’ll discover our Laravel Blogs to be a valuable resource.

Leave a Comment