Laravel Socialite LinkedIn Login

To implement LinkedIn login using Laravel Socialite, follow these steps:


Step 1: Install Laravel Socialite

In your terminal, run the following command to install the Socialite package:


composer require laravel/socialite


Step 2: Configure LinkedIn OAuth Credentials

Visit the LinkedIn Developer Console (https://www.linkedin.com/developers/apps), create a new app, and obtain the Client ID and Client Secret.


Step 3: Configure Services in Laravel

Open `config/services.php` and add the LinkedIn OAuth credentials:


'linkedin' => [

    'client_id' => env('LINKEDIN_CLIENT_ID'),

    'client_secret' => env('LINKEDIN_CLIENT_SECRET'),

    'redirect' => env('LINKEDIN_REDIRECT_URI'),

],


Step 4: Create Routes

In your `routes/web.php`, create routes for social login:


Route::get('auth/linkedin', 'Auth\SocialController@redirectToLinkedIn');

Route::get('auth/linkedin/callback', 'Auth\SocialController@handleLinkedInCallback');


Step 5: Create Controller

Generate a controller using the following command:


php artisan make:controller Auth/SocialController


In `Auth/SocialController.php`, implement the following methods:


use Laravel\Socialite\Facades\Socialite;


public function redirectToLinkedIn()

{

    return Socialite::driver('linkedin')->redirect();

}


public function handleLinkedInCallback()

{

    $user = Socialite::driver('linkedin')->user();


    // $user contains user details received from LinkedIn


    // You can now authenticate the user or perform other actions


    return redirect()->route('home'); // Redirect after successful login

}


Step 6: Update .env File

Add your LinkedIn OAuth credentials to your `.env` file:


LINKEDIN_CLIENT_ID=your_linkedin_client_id

LINKEDIN_CLIENT_SECRET=your_linkedin_client_secret

LINKEDIN_REDIRECT_URI=http://your-app-url/auth/linkedin/callback


Replace `your_linkedin_client_id` and `your_linkedin_client_secret` with your actual credentials.


Step 7: Implement Login Button

In your view, create a link/button to initiate the LinkedIn login process:


<a href="{{ url('auth/linkedin') }}">Login with LinkedIn</a>


That's it! Users can now click the "Login with LinkedIn" link/button, and they will be redirected to LinkedIn for authentication. Upon successful authentication, they will be redirected back to your application with user data.


Remember to handle user data appropriately, possibly by creating a new user or authenticating an existing user in your database. Also, customize the redirection and user management as per your application's requirements.