In this tutorial you will learn about the Laravel 7/6 socialite Linkedin Login Example and its application with practical example.
In this Laravel 7/6 socialite Linkedin Login tutorial I’ll show you how to integrate socialite LinkedIn social login in laravel application. In this tutorial you will learn to integrate LinkedIn login in laravel. In this article we will integrate login with LinkedIn in laravel application. This tutorial is step by step guide for you on how to integrate linkedin social login in laravel using socialite package.
- Laravel 7/6 socialite Linkedin Login Example
- Step 1: Install the laravel App
- Step 2: Connect App to Database
- Step 3: Download Socialite Package
- Step 4: Create a Linkedin App
- Step 5: Add Code in Migration, Model File
- Step 6: Run Migration
- Step 7: Add Routes for Linkedin Login
- Step 8: Create Controller
- Step 9: Generate Bootstrap Auth File
- Step 10: Add Linkedin Login In Blade View
- Step 11: Run Development Server
As we all know that users are not much interested in filling up long registration form to register with any application. Allowing users to login with their social media accounts is quick and powerful way to get registered/verified users for your laravel application. Allowing users to login with their social media accounts makes registration/login process much easier, it also encourages more users to register for your application. In this step by step tutorial, you will learn to integrate linkedin login with your laravel application.
Laravel 7/6 socialite Linkedin Login Example
- Step 1: Install Laravel App
- Step 2: Connect App to Database
- Step 3: Install Socialite Package
- Step 4: Create a Linkedin App
- Step 5: Add Code in Migration, Model File
- Step 6 Run Migration
- Step 7: Add Routes for Linkedin Login
- Step 8: Create Controller
- Step 9: Generate Bootstrap Auth File
- Step 10: Add Linkedin Login In Blade View
- Step 11: Run Development Server
Step 1: Install the laravel App
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Connect App to Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Step 3: Download Socialite Package
In this step we will Install Socialite Package via Composer using following command:
1 |
composer require laravel/socialite |
After Installing ‘socialite’ package, we need to add service provider and alias in config/app.php file as following.
config/app.php
1 2 3 4 5 6 7 8 9 |
'providers' => [ // Other service providers… Laravel\Socialite\SocialiteServiceProvider::class, ], 'aliases' => [ // Other aliases… 'Socialite' => Laravel\Socialite\Facades\Socialite::class, ], |
Step 4: Create a Linkedin App
In this step, we will create linkedin app for client and secret key, go to following link
1 |
https://www.linkedin.com/developers/apps/new |
Now create linkedin app filling the details and create your linkedin app. After creating the app set the redirect URL. Now, copy the client id and secret from linkedin app dashboard. Now, go to your project config directory and open service.php file. Then add linkedin app details in service.php file as following:
1 2 3 4 5 |
'linkedin' => [ 'client_id' => 'xxxx', 'client_secret' => 'xxx', 'redirect' => 'http://127.0.0.1:8000/callback/linkedin', ], |
Step 5: Add Code in Migration, Model File
Now, go to database/migrations directory and open create_users_table.php. Then put the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique()->nullable(); $table->string('provider'); $table->string('provider_id'); $table->timestamp('email_verified_at')->nullable(); $table->string('password')->nullable(); $table->rememberToken()->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } } |
Go to app/User.php and add fillable property like following in User.php model file:
1 2 3 |
protected $fillable = [ 'name', 'email', 'password', 'provider', 'provider_id' ]; |
Step 6: Run Migration
Before you run PHP artisan migrate command go to app/providers/AppServiceProvider.php and put the following code in it:
1 2 3 4 5 6 7 8 9 |
... use Illuminate\Support\Facades\Schema; .... function boot() { Schema::defaultStringLength(191); } ... |
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
Step 7: Add Routes for Linkedin Login
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 |
Route::get('/auth/redirect/{provider}', 'LinkedinSocialController@redirect'); Route::get('/callback/{provider}', 'LinkedinSocialController@callback'); |
Step 8: Create Controller
Now, lets create a controller named LinkedinSocialController using command given below –
1 |
php artisan make:controller LinkedinSocialController |
After creating controller go to app/http/controllers directory and open LinkedinSocialController.php file. And put the following code into LinkedinSocialController.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Validator,Redirect,Response,File; use Socialite; use App\User; class LinkedinSocialController extends Controller { public function redirect($provider) { return Socialite::driver($provider)->redirect(); } public function callback($provider) { $getInfo = Socialite::driver($provider)->user(); $user = $this->createUser($getInfo,$provider); auth()->login($user); return redirect()->to('/home'); } function createUser($getInfo,$provider){ $user = User::where('provider_id', $getInfo->id)->first(); if (!$user) { $user = User::create([ 'name' => $getInfo->name, 'email' => $getInfo->email, 'provider' => $provider, 'provider_id' => $getInfo->id ]); } return $user; } } |
Step 9: Generate Bootstrap Auth File
Install Laravel UI
1 |
composer require laravel/ui |
Create Auth
1 |
php artisan ui bootstrap --auth |
NPM Install
1 |
npm install |
Step 10: Add Linkedin Login In Blade View
In this step, you need to add two buttons in laravel blade view for social LinkedIn login. So go to Resources/Views/Auth/register.blade.php and add a Linkedin social login button
1 2 3 4 5 6 |
<hr> <div class="form-group row mb-0"> <div class="col-md-8 offset-md-4"> <a href="{{ url('/auth/redirect/linkedin') }}" class="btn btn-primary"><i class="fa fa-linkedin"></i> Linkedin</a> </div> </div> |
Then, go to Resources/Views/Auth/login.blade.php and add a LinkedIn social login button:
1 2 3 4 5 6 |
<hr> <div class="form-group row mb-0"> <div class="col-md-8 offset-md-4"> <a href="{{ url('/auth/redirect/linkedin') }}" class="btn btn-primary"><i class="fa fa-linkedin"></i> Linkedin</a> </div> </div> |
Step 11: Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 2 3 4 5 |
http://127.0.0.1:8000/login Or direct hit in your browser http://localhost/blog/public/login |