Creating Social Login In Laravel Using Socialite

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.
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.
Now Socialite is setup in your app. Let’s now handle our Login controller to handle social login request.

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.

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.

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.
Now replace name and email form group div with these.
                     
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.
Similar way you can add other social networks in it.
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.

Summary:

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.

Comments