Categories: Learn Laravel Tutorials, Tips And Guides

Creating Social Login in Laravel using Socialite

Social login button are becoming an essential part of any site which perform user login and registration. Its saves users time, since they won’t need to fill the whole form they just sign up with their social profile and next time they can loggedin in website by a single click.

There are many different sites and packages which you can integrate on your site to provide social login functionality. Laravel has released its own package name Socialite which you can use in your projects. Currently socialite support following social logins:

  • Facebook
  • Twitter
  • LinkedIn
  • Github
  • Google
  • Bitbucket

In this tutorial I am going to integrate Laravel Socialite and going to register a new user using it. I am going to integrate Facebook login in this blog. You can easily integrate other social logins using the same technique which I will discuss in this blog. Let’s get started.

Installing and Setting Up Socialite

Now in your laravel project run the following command to install Laravel Socialite.

composer require laravel/socialite

Once the package is installed open ‘app/config.php’ file and add the following line in `providers` array.


Now add the following line in `aliases` array.

'Socialite' => Laravel\Socialite\Facades\Socialite::class,

Now Socialite is setup in your app. Let’s now handle our Login controller to handle social login request.

Related: How To Setup Laravel Login Authentication In Simple And Easy Steps

Modifying LoginController

In this tutorial I am keeping in mind that you have created an auth system using laravel auth command. Now open ‘app/Http/Controllers/Auth/LoginController.php’ file. Now add the following methods in it.



    * Handle Social login request


    * @return response


   public function socialLogin($social)


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



    * Obtain the user information from Social Logged in.

    * @param $social

    * @return Response


   public function handleProviderCallback($social)


       $userSocial = Socialite::driver($social)->user();

       $user = User::where(['email' => $userSocial->getEmail()])->first();



           return redirect()->action('HomeController@index');


           return view('auth.register',['name' => $userSocial->getName(), 'email' => $userSocial->getEmail()]);



In the above code we have created a `socialLogin` method which will redirect user to the appropriate social login page i.e. Twitter,Facebook,etc. The second method `handleProviderCallback($social)` will work as a callback function. The method first get the details of the user who have sign in using a social network then it checks whether the user is already exists in users table or not. If the user exists it authenticates it and redirect the user to home. If the user is not exists he will be redirected to register page along with his name and email.  Now I have created methods to handle social logins let’s now create routes for it.

Creating Routes for Social Login in Laravel

Now open routes/web.php file and add the following routes in it.



Now we have set up routes let’ now update our login and register page to handle social login requests.

Updating Login Page

For social login buttons I am going to use Social Buttons for bootstrap. Now open resources/views/auth/login.blade.php and add a following div in form after csrf field.

<div class="form-group">

                           <label >

Updating Register Page

In register page we need to do two things. First we need to add Social Login Buttons and second we need to handle the user data comes from social logged in. First add the following div after csrf field.
<div class="form-group">

                           <label >
Now replace name and email form group div with these.

  <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">

                           <label >
Now I have modified everything let’s now setup Facebook Sign In. 

Setting Up Facebook Sign-in

Now first head to https://developers.facebook.com and create a new app. Once the app is created you will get a api key and secret. Then you need to add a new Facebook Login product. And define your callback their. To learn more about it click here. Now open app/services.php file and add the following line in it.
'facebook' => [

   'client_id' => '1120224634748024', //Facebook API

   'client_secret' => '7303691d807eaff92bbb1657b96b1e', //Facebook Secret

   'redirect' => 'http://laravel.localhost/login/facebook/callback',


Similar way you can add other social networks in it.

'github' => [

   'client_id' => 'af54e6bae9a4edfc6cbb',

   'client_secret' => '95afc7b40e6aca6419a7623c34bb6d86cb2ac',

   'redirect' => 'http://laravel.localhost/login/github/callback',


'twitter' => [

   'client_id' => '8JS1GvRflY5g9N3kZc0heYTqL',

   'client_secret' => 'eYujptPLBAAzdHIz8hiqGgz4MkJTcmL1JAGEuEZsGU1MykjK',

   'redirect' => 'http://laravel.localhost/login/twitter/callback',


Now we have done everything let’s now register a user using facebook.

Registering a User using Facebook

Now in your browser head to login or register page.

Now click on facebook button. You will be redirected to facebook. Once you accepted the permission you will be redirected to register page since the user is not registered. But you will notice that name and email is already been filled user only need to add its password.

Enter the password and click on register. You will be redirected to home. Now logout the user. And again go to login page. Now on login again click on facebook login button. Now you will be directly redirected to the homepage.


In this article we have implemented Laravel Socialite in our project. And used user Facebook profile to logged him and register him on our site.

Ahmed Khan :Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com