Adding a social login system on the website is a trend nowadays. The social login option on a website skips the verification process of a user. Social sites verified users already so your application doesn’t need to perform the verification process again. We can rely on social sites and allow the user to use the system if they log in through a social site. Facebook, Twitter, Google, and LinkedIn are the popular options that are commonly used for social login on the website.
In this article, we will cover a login with LinkedIn in Laravel. If you are looking for a Google or Twitter login then follow the linked articles.
Basic Configuration
For our tutorial, I will integrate LinkedIn login on the fresh Laravel installation. I am going to install Laravel, create a ‘users’ table, and install the package required to integrate the social login system.
Run the command below to install Laravel. Here I am creating a project called ‘linkedinlogin’.
composer create-project laravel/laravel linkedinlogin
After installation, create the database and add the credentials to the .env
file. We are building a login system that requires storing user details in the database.
When a user sign-in through LinkedIn, we will get the user’s LinkedIn profile ID. To store this id I am adding 2 columns – provider
and provider_id
. The provider
column will store the value ‘linkedin’ and provider_id
will store the user’s social id(LinkedIn profile ID).
In the case of social login, we don’t need to store passwords. The password column should have a ‘NULL’ value by default. There is no guarantee of getting a user’s email after social login, so we should set the default value of the email column as ‘NULL’.
Open the users
table migration file and add the code below.
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password')->nullable();
$table->string('provider');
$table->string('provider_id');
$table->rememberToken();
$table->timestamps();
});
}
Next, run the command below to create tables in the database.
php artisan migrate
We also need to add the provider
and provider_id
columns to the User model.
app/Models/User.php
protected $fillable = [
'name', 'email', 'password', 'provider', 'provider_id'
];
Create LinkedIn Application
To get started, you require to create the application on your LinkedIn account. For integration with Laravel, we need the client id and client secret of the LinkedIn application. Below are the steps to follow.
- Go to LinkedIn Developer Network.
- Click on the ‘Create Application’ button.
- Complete the information on the form.
- Add
http://localhost:8000/linkedin/callback
in the Authorized Redirect URLs field. - Copy the Client ID and Client Secret keys.
After these steps, add the product ‘Sign in with LinkedIn’ to your LinkedIn application. Click on the ‘products’ tab and choose ‘Sign In with LinkedIn’. Upon selecting this product, it will go for review and then be included as an added product. It may take some time to review. In my case, it took around 10 minutes.
Create Routes and Controller
We can now start adding code in Laravel. Create a LoginController
using the command:
php artisan make:controller LoginController
Next, create a login.blade
file and add the following HTML to it.
<a href="{{ url('/login/linkedin') }}">
{{ __('Login with LinkedIn') }}
</a>
Call this view from the index
method of LoginController
as follows.
public function index()
{
return view('login');
}
Let’s now define the routes which perform the social login flow for the Laravel application.
Route::get('login', 'LoginController@index');
Route::get('login/{provider}', 'LoginController@redirectToProvider');
Route::get('{provider}/callback', 'LoginController@handleProviderCallback');
Route::get('/home', function () {
return 'User is logged in';
});
login
: This route will call the view.login/{provider}
: The {provider} will be replaced by linkedin. It redirects users to the LinkedIn login page.{provider}/callback
: The user will redirect to this route after authentication with LinkedIn.home
: User will redirect to this route after successful login with LinkedIn. You can change this route depending on your flow.
Login with LinkedIn in Laravel
Laravel has an official package called Socialite. This package helps us to easily integrate the social login system in Laravel. It comes with several adapters – Facebook, Twitter, LinkedIn, Google, etc. All you need to do is follow the Socialite configuration steps. The OAuth flow is handled by the package itself.
Install this package using the command:
composer require laravel/socialite
Upon library installation, open the config/services.php
and add LinkedIn configuration as follows:
'linkedin' => [
'client_id' => env('LINKEDIN_CLIENT_ID'),
'client_secret' => env('LINKEDIN_CLIENT_SECRET'),
'redirect' => env('LINKEDIN_CALLBACK_URL'),
],
Now, add the LinkedIn credentials in the .env
file.
LINKEDIN_CLIENT_ID=YOUR_CLIENT_ID
LINKEDIN_CLIENT_SECRET=YOUR_CLIENT_SECRET
LINKEDIN_CALLBACK_URL=http://localhost:8000/linkedin/callback
Make sure to replace placeholders with their actual values. The final adjustments will go in the LoginController.php
file. In this file, we are writing methods that redirect users to the LinkedIn login, and on successful authorization, it will log the user in.
LoginController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Socialite;
use App\Models\User;
use Auth;
class LoginController extends Controller
{
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Call the view
*/
public function index()
{
return view('login');
}
/**
* Redirect the user to the Linkedin authentication page.
*
* @return Response
*/
public function redirectToProvider($provider)
{
return Socialite::driver($provider)->scopes(['r_liteprofile', 'r_emailaddress'])->redirect();
}
/**
* Obtain the user information from Linkedin.
*
* @return Response
*/
public function handleProviderCallback($provider)
{
$user = Socialite::driver($provider)->user();
$authUser = $this->findOrCreateUser($user, $provider);
Auth::login($authUser, true);
return redirect($this->redirectTo);
}
/**
* If a user has registered before using social auth, return the user
* else, create a new user object.
* @param $user Socialite user object
* @param $provider Social auth provider
* @return User
*/
public function findOrCreateUser($user, $provider)
{
$authUser = User::where('provider_id', $user->id)->first();
if ($authUser) {
return $authUser;
}
return User::create([
'name' => $user->name,
'email' => $user->email,
'provider' => $provider,
'provider_id' => $user->id
]);
}
}
In the LoginController, we set the scopes ['r_liteprofile', 'r_emailaddress']
which are required to fetch basic information of a LinkedIn user. In the callback method, we are checking if the user exists and if it is not then add the user to the database.
I hope you understand how to Login with LinkedIn in Laravel. I would like to hear your thoughts and suggestions in the comment section below.
Related Articles
- Google Login in Laravel Using Laravel Socialite
- How to Add Google OAuth Login in Website with PHP
- Create Multi Language Website In PHP Using Laravel Framework
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.
It doesn’t work bro with laravel 5.8 and socialite version 3.1