Adding LinkedIn Authentication to a Laravel Website

With social authentication, users do not have to fill out forms in order to register on your website; they also do not need to remember another password in other to access their accounts on your website. This article will provide a step-by-step process to add LinkedIn authentication to your website

Assumptions:

This tutorial assumes that you have PHP and Composer installed on your computer. It also uses XAMPP’s Apache and MySQL installations. It is therefore assumed that the reader is familiar with XAMPP. Visit the Composer and XAMPP websites to download them.

1. Create a new Laravel project

composer create-project laravel/laravel social-auth

2. Configure your database setup

  1. Create a new database called “social-auth” in phpMyAdmin
  2. Edit your .env file with the appropriate details as shown below

3. Setup your basic authentication system with Laravel Breeze

To setup Laravel Breeze, run the following lines in the order they appear on your command line:

composer require laravel/breeze --devphp artisan breeze:installnpm installnpm run devphp artisan migrate

4. Install Laravel Socialite and add providers and aliases in your config file

composer require laravel/socialite

In your config/app.php, add Socialite to your providers and aliases arrays thus:

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

5. Create a new LinkedIn app

  1. Click the “Create app” button

2. When the next page appears, provide the appropriate details and click “Create app”

3. On the page that appears, click the “Auth” tab in the navbar and you will get a page that looks like this.

Add a LinkedIn entry to your config/services.php file.

return [
....
....
....
'linkedin' => [
'client_id' => env('LINKEDIN_CLIENT_ID'),
'client_secret' => env('lINKEDIN_CLIENT_SECRET'),
'redirect' => env('LINKEDIN_CLIENT_REDIERECT'),
],
]

Copy your client id and secret and add them to your .env file as shown below

LINKEDIN_CLIENT_ID="Your LinkedIn app id"
LINKEDIN_CLIENT_SECRET="Your LinkedIn client secret"
LINKEDIN_CLIENT_REDIERECT="http://localhost:8000/auth/linkedin/callback"

4. On the same page, add the redirect URL above as shown below and click the “Update” button.

6. Add a “linkedin_id” column to your “users” table

php artisan make:migration add_linkedin_id_to_users_table

Make the following changes to the file created.

Run the following line to update your database

php artisan migrate:refresh

8. Setup your Linkedin authentication routes

use App\Http\Controllers\SocialController;//Linkedin Authentication RoutesRoute::get('auth/linkedin', [SocialController::class, 'linkedinRedirect'])->name('login.linkedin');Route::get('auth/linkedin/callback', [SocialController::class, 'linkedinCallback']);

9. Create and configure controller to handle the routes

php artisan make:controller SocialController

Define linkedinRedirect() and linkedinCallback() methods as shown below:

10. Add Linkedin login button to your registration and login pages

<div class="flex items-center justify-center mt-4">
<a href="{{ route('login.linkedin') }}" class="underline text-sm text-gray-600 hover:text-gray-900">
Login With Linkedin
</a>
</div>

11. Voila! You are done !!