In most sites today, we found social login facility,where users have the option of signing up with a social provider such as Facebook, Google, Twitter etc. Laravel has released its own package name Socialite for social login. We can integrate Socialite in our laravel project for social media login.

Currently socialite support following social logins:

1) Facebook

2) Twitter

3) LinkedIn

4) Github

5) Google

6) Bitbucket

Laravel Social Login using socialite

First of all we have to download a package, laravel socialite.
Laravel Socialite is a package developed to abstract away any social authentication. It is an expressive, fluent interface to OAuth authentication. Socialite only supports Google, Facebook, Twitter, LinkedIn, Github, and Bitbucket as OAuth providers

https://developers.facebook.com/apps
https://apps.twitter.com/
https://www.linkedin.com/developer/apps/

https://developers.google.com/

To download the socialite package use the following the command

composer require laravel/socialite

 

Now open your view file(view->login.blade.php) and add the following lines of code

<a href="{{ url('/login/twitter') }}"><button class="twitter">Sign in with Twitter</button></a>

<a href="{{ url('/login/facebook') }}"><button class="facebook">Sign in with Facebook</button></a>

<a href="{{ url('/login/linkedin') }}"><button class="linkedin">Sign in with Linkedin</button></a>

<a href="{{ url('/login/google') }}"><button class="google">Sign in with Google</button></a>

Now open your app.php file in Config->app.php and add

'providers'=>[

Laravel\Socialite\SocialiteServiceProvider::class in provider section

],
'aliases'=>[
'Socialite' => Laravel\Socialite\Facades\Socialite::class  in alises

],

Now add the route in your routes->web.php like below

Route::get('/login/{social}','Auth\LoginController@socialLogin')->where('social','twitter|facebook|linkedin|google');

Route::get('/login/{social}/callback','Auth\LoginController@handleProviderCallback')->where('social','twitter|facebook|linkedin|google');

Now write the following functions in auth-> login controler

use Socialite;
use App\User;

public function socialLogin($social)

{

return Socialite::driver($social)->redirect();
}

public function handleProviderCallback($provider)

{

$user = Socialite::driver($provider)->user();

$authUser = $this->findOrCreateUser($user, $provider);

Auth::login($authUser, true);
return redirect($this->redirectTo);

}

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,
'profile_image' => $user->avatar
]);
}

Now add the client id, client secret , callback urls in config > service.php like below

'twitter' => [
'client_id' => ' Your client id',
'client_secret' => ' your client secret',
'redirect' => 'http://localhost:8000/login/twitter/callback',
],
'facebook' => [
'client_id' => '',
'client_secret' => '',
'redirect' => 'http://localhost:8000/login/facebook/callback',
],
'linkedin' => [
'client_id' => '',
'client_secret' => '',
'redirect' => 'http://localhost:8000/login/linkedin/callback',
],
'google' => [
'client_id' => '',
'client_secret' => '',
'redirect' => 'http://localhost:8000/login/google/callback',
]

Now in user model add the fallible fields

protected $fillable = [
'name', 'email', 'password','status', 'provider', 'provider_id','profile_image'
];


2 thoughts on “Laravel social login using Socialite

    • Dear Debojit,
      Thank you for writing , I am going to write soon a fresh article how to create social login accounts like Facebook, Twitter, LinkedIn, Google, etc.

Leave comment

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