Skip to content

Webtuts

Online Web Tutorial

  • Laravel
  • Laravel 8
  • jQuery
  • CodeIgniter
  • CodeIgniter 4

Laravel 7 Login with Linkedin using Socialite

February 14, 2021 web-tuts Laravel

In this example i explain Laravel 7 Login with Linkedin using Socialite. Also  laravel 7 socialite provide api to login with Linkedin account. We can easily login using Linkedin account in laravel project. So in this post we will login with Linkedin.

Larave 7 Login with Linkedin account is a become more and more popular in the world. Every account is link by Linkedin account and google account.  Laravel 7 provide us Socialite package that is help to social authentication.

Laravel 7 Login with Linkedin Example

Step 1: Install Laravel 7

In this step i install fresh laravel 7 application and setup laravel 7 application. So run the following command to get laravel 7 application.

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

Step 2: Install Laravel Socialite

In this step i install laravel socialite package that provide api to connect with Linkedin account. So run the following command on your terminal ang get laravel socialite package.

composer require laravel/socialite

After install the laravel socialite package we add providers and aliases in config file, So open your config/app.php file and add the following service provider and aliase.

'providers' => [
    ....
    Laravel\Socialite\SocialiteServiceProvider::class,
],
'aliases' => [
    ....
    'Socialite' => Laravel\Socialite\Facades\Socialite::class,
],

Step 3: Create Linkedin App

In this step we require Linkedin client id and secret that way we can get other user information. so first you create Linkedin app account then you can create from here : Linkedin Developers Apps.

Read Also :  How to Send Email with Attachment in Laravel 8

After create account you can copy your client id and secret.

Now you have to set your app id, secret and call back url in config file so open the first config/services.php and set id and secret following code:

return [
    ....
    'linkedin' => [
        'client_id' => 'id',
        'client_secret' => 'secret',
        'redirect' => 'http://learnl52.hd/auth/linkedin/callback'
    ],
]

Step 4: Create Auth

In this step we install laravel ui and generate auth in laravel 7 application so, let’s run following command in your terminal:

Install Laravel UI (User Interface)

composer require laravel/ui

Create Auth:

php artisan ui bootstrap --auth

NPM Install:

npm install

Run NPM:

npm run dev

Step 5: Add Database Column

In this step i create migration for add linkedin_id in your user table. So first run bellow command:

php artisan make:migration add_linkedin_id_column
Migration : add_linkedin_id_column
Schema::table('users', function ($table) {
    $table->string('linkedin_id');
});

app/Http/routes.php

Route::get('linkedin', function () {
    return view('linkedinAuth');
});
Route::get('auth/linkedin', 'Auth\AuthController@redirectToLinkedin');
Route::get('auth/linkedin/callback', 'Auth\AuthController@handleLinkedinCallback');

Step 6: Create Controller

In this step we create controller and add method of Linkedin auth that method will handle Linkedin callback url and etc.

Read Also :  Laravel Arr sort() function Example

app/Http/Controllers/Auth/AuthController.php

namespace App\Http\Controllers\Auth;


use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Socialite;
use Auth;
use Exception;


class AuthController extends Controller
{


    use AuthenticatesAndRegistersUsers, ThrottlesLogins;


    protected $redirectTo = '/';


    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }


    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }


    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }


    public function redirectToLinkedin()
    {
        return Socialite::driver('linkedin')->redirect();
    }


    public function handleLinkedinCallback()
    {
        try {
            $user = Socialite::driver('linkedin')->user();
            $create['name'] = $user->name;
            $create['email'] = $user->email;
            $create['linkedin_id'] = $user->id;
            
            $userModel = new User;
            $createdUser = $userModel->addNew($create);
            Auth::loginUsingId($createdUser->id);
            return redirect()->route('home');
        } catch (Exception $e) {
            return redirect('auth/linkedin');
        }
    }
}

resources/views/linkedinAuth.blade.php

@extends('layouts.app')
@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Login</div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">
                        {!! csrf_field() !!}


                        <div class="form-group">
                            <label class="col-md-4 control-label">E-Mail Address</label>


                            <div class="col-md-6">
                                <input type="email" class="form-control" name="email" value="{{ old('email') }}">
                            </div>
                        </div>


                        <div class="form-group">
                            <label class="col-md-4 control-label">Password</label>


                            <div class="col-md-6">
                                <input type="password" class="form-control" name="password">
                            </div>
                        </div>


                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="remember"> Remember Me
                                    </label>
                                </div>
                            </div>
                        </div>


                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">


                                <a href="{{ url('auth/linkedin') }}" class="btn btn-primary">
                                    <strong>Login With Linkedin</strong>
                                </a>


                                <button type="submit" class="btn btn-primary">
                                    Login
                                </button>


                                <a class="btn btn-link" href="{{ url('/password/reset') }}">Forgot Your Password?</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

I hope you understand Login with linkedin..

Read Also :  Full Text Search in Laravel Example

Related posts:

  • jQuery Get Image Src and Set Image Src
  • Laravel 7 Socialite Login with Google Account Example
  • How to Flash Message in Laravel Example
  • Laravel 7 Login with Github Example
  • Laravel Where Not in Eloquent Example
  • Laravel Firebase Push Notification Example
  • How to Integrate Login with Facebook in Laravel
  • Laravel Livewire Dependant Dropdown Example
  • Laravel 8 Crud Operations Example
  • Laravel Multiple Checkbox with Delete Rows Example
AuthAuthenticationLaravelLaravel 6Laravel 7Laravel Linkedin LoginLinkedin APISocialite

Post navigation

Previous Post:Laravel 7 Socialite Login with Google Account Example
Next Post:Laravel Livewire Change Event Example

Recent Posts

  • How To Seed Data In Laravel 9
  • Marge Multiple PDF In Laravel 9
  • Laravel 9 CRUD Operation Tutorial For Beginner
  • Laravel 9 Generate PDF File using DomPDF
  • Laravel 9 Mail | Laravel 9 Send Email Example
  • Laravel 9 Create Zip File And Download
  • Laravel 9 Socialite Login with Google Example
  • Laravel 9 Image Upload Example
  • How to Get Client IP Address in Laravel 9?
  • Laravel 8 Print Last Query Example
  • Laravel Select with Sum Query Example
  • Laravel 8 Auth Login with Username or Email
  • Laravel 8 Find Nearest Location By Latitude and Longitude
  • 404 Not Found but Route Exist in Laravel
  • Laravel 8 Clear Cache of Route, View & Config
  • Laravel 8 Change Password with Current Password Validation
  • Laravel Carbon Get First Day of Specific Month Example
  • Laravel 8 Database Seeder Example
  • Laravel Eloquent doesntHave() Condition Example
  • Restore Back Deleted Records in Laravel 8
  • Laravel 8 Multiple Database Connections
  • Laravel 8 Download File Example
  • PHP Array Push Key Value Example
  • How To Add Days To Current Date In Laravel Blade
  • Moment JS Get Last Date of Month Example
  • Laravel Carbon Convert String to Date Example
  • Moment Get Current Week Number Example
  • Laravel Carbon Get Year from Date Example
  • JavaScript Array Remove Element
  • Laravel Carbon Get Last Day of Month Example
  • How To Validate Upload File Type Using JavaScript
  • How to Create Multilingual Website in Laravel 8
  • Laravel 8 Automatic Database Encryption Decryption
  • How To File Upload In CodeIgniter 4 Example
  • How to Get Month Name from Date in Laravel 8
  • CRUD with Image Upload in Laravel 8 Example
  • CodeIgniter 4 Watermark Image Example
  • jQuery Get All Checked Checkbox Values in Array
  • CodeIgniter 4 Google Bar & Line Charts Example
  • Codeigniter 4 Ajax Image Upload With Preview Example
  • Space Not Allowed Validation in Laravel Example
  • Laravel 8 View Render Example
  • Laravel 8 Form Validation Example
  • Laravel 8 Signature Pad Example
  • Laravel 8 cURL HTTP Request Example
  • Laravel 8 Datatables Filter with Dropdown
  • Laravel 8 Pagination Example
  • Laravel 8 QR Code Generate Example
  • Laravel 8 Highcharts Example
  • Laravel 8 Multi Step Form Example
© Copyright 2020 Web-tuts.com. All rights reserved. Home Privacy Policy Contact Us Terms of Use