To implement LinkedIn login in Laravel 5 using the Socialite package, you need to follow these steps:

  1. Install the Socialite Package: If you haven't already, install the Socialite package via Composer:

    bash
    composer require laravel/socialite
  2. Configure API Credentials: Obtain your LinkedIn API credentials by creating an application on the LinkedIn Developer Portal: https://www.linkedin.com/developers/

    Once you have your client_id, client_secret, and redirect URL, add them to your config/services.php file:

    php
    // config/services.php 'linkedin' => [ 'client_id' => env('LINKEDIN_CLIENT_ID'), 'client_secret' => env('LINKEDIN_CLIENT_SECRET'), 'redirect' => env('LINKEDIN_REDIRECT_URI'), ],

    Make sure to set the corresponding environment variables in your .env file.

  3. Implement LinkedIn Login Redirect Route: Create a route that redirects the user to LinkedIn for authentication:

    php
    // routes/web.php Route::get('/auth/linkedin', 'Auth\LoginController@redirectToLinkedIn');
  4. Implement the Controller Method: In your controller (e.g., Auth\LoginController), create the method that redirects the user to the LinkedIn login page:

    php
    // app/Http/Controllers/Auth/LoginController.php use Socialite; class LoginController extends Controller { public function redirectToLinkedIn() { return Socialite::driver('linkedin')->redirect(); } }
  5. Handle the LinkedIn Callback: Create a route to handle the callback from LinkedIn and obtain the user's information:

    php
    // routes/web.php Route::get('/auth/linkedin/callback', 'Auth\LoginController@handleLinkedInCallback');
  6. Implement the Callback Method: In your controller, create the method that handles the LinkedIn callback and fetches the user's data:

    php
    // app/Http/Controllers/Auth/LoginController.php use Socialite; class LoginController extends Controller { public function handleLinkedInCallback() { $user = Socialite::driver('linkedin')->user(); // You can now access the user's data from $user // For example: $user->getId(), $user->getName(), etc. // Implement your authentication and user creation logic here } }
  7. Implement User Authentication and Registration: Implement your custom authentication and user registration logic based on the information obtained from the LinkedIn callback.

That's it! Now, when a user accesses the /auth/linkedin route, they will be redirected to the LinkedIn login page for authentication. After successful login, the user will be redirected back to the /auth/linkedin/callback route, where you can obtain the user's data and handle the authentication and registration process accordingly.

Have questions or queries?
Get in Touch