Laravel 6 Socialite Linkedin Login Tutorial Example – onlinecode

Laravel 6 Socialite Linkedin Login Tutorial Example – onlinecode

In this post we will give you information about Laravel 6 Socialite Linkedin Login Tutorial Example – onlinecode. Hear we will give you detail about Laravel 6 Socialite Linkedin Login Tutorial Example – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this tutorial, we will tell you how to create a Linkedin Login in Laravel Framework(Laravel 6 Socialite Linkedin Login Tutorial Example).

Normally, we have seen that many websites are using Linkedin social login. but here we can easily create the Linkedin social login using the socialite package. so you can follow the below step.

Overview

Step 1: Install Laravel

Step 2: Setting Database Configuration

Step 3: Create Table using migration

Step 4: Install Package

Step 5: Add providers and aliases

Step 6: Create a Linkedin App

Step 7: Configuration of API Key

Step 8: Create Auth

Step 9: Create Route

Step 10: Update Model and Controller

Step 11: Create Blade File

Step 1: Install Laravel

We are going to install laravel 6, so first open the command prompt or terminal and go to go to xampp htdocs folder directory using the command prompt. after then run the below command.

composer create-project --prefer-dist laravel/laravel larave6_linkedin_login

Step 2: Setting Database Configuration

After complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name(laravel6_linkedin_login)
DB_USERNAME=Enter_Your_Database_Username(root)
DB_PASSWORD=Enter_Your_Database_Password(root)

Step 3: Create Table using migration

Now, we need to add linkedin_id in the user table. so first we will add the linkedin_id in the migration list after then we will run the migration command.

Here this file We have already updated in the database/migrations/create_users_table file. so you can see the below example code.

<?php use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
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();
            $table->string('password');
            $table->string('linkedin_id')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });
    }
    /** * Reverse the migrations. * * @return void */
    public function down() {
        Schema::dropIfExists('users');
    }
} ?>

Run the below command. after the changes above file.

php artisan migrate

Step 4: Install Package

Now, we are going to install the Socialite package using the below command.

composer require laravel/socialite

Step 5: Add providers and aliases

We will add below providers and aliases in the “config/app.php” file

'providers' => [
 
	....
 
	LaravelSocialiteSocialiteServiceProvider::class,
 
],
 
'aliases' => [
 
	....
 
	'Socialite' => LaravelSocialiteFacadesSocialite::class,
 
]

Step 6: Create Linkedin App

here in this step, we need to Linkedin client id and client secret key and that credentials through we can create successfully login. if you don’t have Linkedin credentials then you have to create a Linkedin app. so you can go on https://www.linkedin.com/developers/apps/new and create it.

Step 7: Configuration of Api Key

Now, we are going to configuration of api key in app/config/services.php file.

'linkedin' => [
    'client_id' => 'enter your client id',
    'client_secret' => 'enter your secret key',
    'redirect' => 'http://127.0.0.1:8000/callback/linkedin',
  ],

Step 8: Create Auth

Here in this step, below command using we will create laravel UI and authentication.

composer require laravel/ui
php artisan ui bootstrap --auth 
npm install 

Step 9: Create Route

Add the following route code in the “routes/web.php” file.

<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/ 
Route::get('/', function () {
    return view('linkedinLogin');
});
Route::get('auth/linkedin', 'AuthLoginController@redirectToLinkedin');
Route::get('auth/linkedin/callback', 'AuthLoginController@handleLinkedinCallback'); 
?>

Step 10: Update Model and Controller

Here in this step, we need to update the User.php model and LoginController.php. so you can see the below example code.

app/User.php

<?php 
namespace App; 
use IlluminateNotificationsNotifiable;
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateFoundationAuthUser as Authenticatable;
class User extends Authenticatable { 
	use Notifiable; 
	/**
     * The attributes that are mass assignable.
     *
     * @var array
     */
	protected $fillable = [ 'name', 'email', 'password', 'linkedin_id' ]; 
	/**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
	protected $hidden = [ 'password', 'remember_token', ]; 
	/**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
	protected $casts = [ 'email_verified_at' => 'datetime', ]; 
} 
	
?>

app/Http/Controllers/Auth/LoginController.php

<?php namespace AppHttpControllersAuth;
use AppHttpControllersController;
use IlluminateFoundationAuthAuthenticatesUsers;
use Socialite;
use Auth;
use Exception;
use AppUser;
class LoginController extends Controller { 
	/*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */
    use AuthenticatesUsers;
    /*** Where to redirect users after login. 
	** @var string 
	*/
    protected $redirectTo = '/home';
    /*** Create a new controller instance. 
	* * @return void 
	*/
    public function __construct() {
        $this->middleware('guest')->except('logout');
    }
    public function redirectToLinkedin() {
        return Socialite::driver('linkedin')->redirect();
    }
    public function handleLinkedinCallback() {
        try {
            $user = Socialite::driver('linkedin')->user();
            $finduser = User::where('linkedin_id', $user->id)->first();
            if ($finduser) {
                Auth::login($finduser);
                return return redirect('/home');
            } else {
                $newUser = User::create(['name' => $user->name, 'email' => $user->email, 'linkedin_id' => $user->id]);
                Auth::login($newUser);
                return redirect()->back();
            }
        }
        catch(Exception $e) {
            return redirect('auth/linkedin');
        }
    }
} ?>

Step 11: Create Blade File

Finally, We will create a linkedinLogin.blade.php file in the “resources/views/” folder directory and paste the below code.linkedinLogin.blade.php

<div >
	<div >
		<div >
			<a  href="{{ url('auth/linkedin') }}">
				<strong>Login With Linkedin</strong>
			</a>
		</div>
	</div>
</div>

Please follow and like us:

Hope this code and post will helped you for implement Laravel 6 Socialite Linkedin Login Tutorial Example – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  13  =  18

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US