Laravel Login with Linkedin using Socialite Package
In this post we will give you information about Laravel Login with Linkedin using Socialite Package. Hear we will give you detail about Laravel Login with Linkedin using Socialite PackageAnd how to use it also give you demo for it if it is necessary.
In my previous post i added login with google and login with facebook, that’s very simple and almost same but if you doing from scratch then it’s important to write all. So in this post we will login with Linkedin. In this post i want to share with you how to do sign in with Linkedin and how to do sign up with Linkedin. Laravel 5 provide very easy way to implement login with your Linkedin account and register with your Linkedin id. Laravel 5 provide us Socialite package that is help to social authentication. In this post we will make example same as like bellow preview and you can do that easily by using few following step:
Preview:
Step 1: Installation
In first step we will install Socialite Package that provide Linkedin api to connect with Linkedin account. So, first open your terminal and run bellow command:
composer require laravel/socialite
After install above package we should add providers and aliases in config file, Now open config/app.php file and add service provider and aliase.
'providers' => [
....
LaravelSocialiteSocialiteServiceProvider::class,
],
'aliases' => [
....
'Socialite' => LaravelSocialiteFacadesSocialite::class,
],
Step 2: Create Linkedin App
In this step we need Linkedin client id and secret that way we can get information of other user. so if you don’t have Linkedin app account then you can create from here : Linkedin Developers App. you can find bellow screen :
Now you have to click on Create Application and fill form details and create new application. after created application you can see following slide:
and you can copy client id and secret.
Now you have to set app id, secret and call back url in config file so open config/services.php and set id and secret this way:
config/services.php
return [
....
'linkedin' => [
'client_id' => 'id',
'client_secret' => 'secret',
'redirect' => 'http://learnl52.hd/auth/linkedin/callback'
],
]
Step 3: Create Linkedin Login
In this step first we have to create migration for add linkedin_id in your user table. so le’s craete new migration and bellow column this way:
Migration
Schema::table('users', function ($table) {
$table->string('linkedin_id');
});
After adding linkedin_id column first we have to add new route for linkedin login. so let’s add bellow route in routes.php file.
app/Http/routes.php
Route::get('linkedin', function () {
return view('linkedinAuth');
});
Route::get('auth/linkedin', 'AuthAuthController@redirectToLinkedin');
Route::get('auth/linkedin/callback', 'AuthAuthController@handleLinkedinCallback');
After add route, we need to add method of linkedin auth that method will handle linkedin callback url and etc, first put bellow code on your AuthController.php file.
app/Http/Controllers/Auth/AuthController.php
namespace AppHttpControllersAuth;
use AppUser;
use Validator;
use AppHttpControllersController;
use IlluminateFoundationAuthThrottlesLogins;
use IlluminateFoundationAuthAuthenticatesAndRegistersUsers;
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');
}
}
}
Ok, now at last we need to add blade view so first create new file linkedinAuth.blade.php file and put bellow code:
resources/views/linkedinAuth.blade.php
@extends('layouts.app')
@section('content')
<div >
<div >
<div >
<div >
<div >Login</div>
<div >
<form role="form" method="POST" action="{{ url('/login') }}">
{!! csrf_field() !!}
<div >
<label >E-Mail Address</label>
<div >
<input type="email" name="email" value="{{ old('email') }}">
</div>
</div>
<div >
<label >Password</label>
<div >
<input type="password" name="password">
</div>
</div>
<div >
<div >
<div >
<label>
<input type="checkbox" name="remember"> Remember Me
</label>
</div>
</div>
</div>
<div >
<div >
<a href="{{ url('auth/linkedin') }}" >
<strong>Login With Linkedin</strong>
</a>
<button type="submit" >
Login
</button>
<a href="{{ url('/password/reset') }}">Forgot Your Password?</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Ok, now you are ready to use open your browser and check here : URL + ‘/linkedin’.
Hope this code and post will helped you for implement Laravel Login with Linkedin using Socialite Package. 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