Social Login in Laravel 5.6 using Socialite

social login in laravel kodementor

Social login is becoming essential for any web applications that require registration and login. Social login helps in the faster registration process and valid emails. Today, we are going to integrate laravel/socialite package in our application. Laravel provides built-in authentication. We are going to modify the same according to our need. So, let’s get started.

1Installing Laravel and Setting up

First of all, we will install a fresh laravel 5.6 application. To do this, we will run below command:

Setup Database

This will create a project with latest version of laravel. Now, lets setup our database. Open your .env file (If you don’t see any, make a duplicate of .env.example file and rename it to .env), and enter your database details. My setup looks like below:

2Creating default Authentication

Laravel provides default authentication system. It automatically creates routes, controller and views for us. Thus it makes us easy. We will modify this default authentication. To create authentication in our application, run the following command:

3Create migrations and tables

We have our login registration scafilding, now we will create migrations for table. Go to database/migrations directory and open create_users_table and add column to store provider id. Your final migration looks like below:

After your configuration is complete, migrate the migration. Simply run the following command.

4Configure User Model

We have successfully migrated our tables but we need to make the column fullable. To do this, simply add the following code in your app/User.php

5Install Socialite package and Configure

Now, we are on our real business. Let’s install socialite package using composer. To do this, run the following command.

This will download all the dependencies of this package in your vendor folder. Now, you we need to register service provider for this. Add service provider class in config/app.php file:

Similarly, we need to add alias in this file as below:

6Register Provider in Application

After successfully installing our package, its time to configure our provider setting in our environment. You can learn how to get provider credentials i.e. client id, client secret and redirect url from this article.

We will use Facebook, Twitter and Google login for this tutorial purpose. Add the following details in your .env file.

Now open app/services.php file and add the following configurations in it.

Note: Above credentials are for demo purpose. You need to get your own credentials

7Configure Route

Now, it’s time to setup our routes. You need two routes, one for redirecting user to the provider and another for receiving callback from provider.

8Create Controller and Modify it

You can handle the login with login controller but I prefer that social login is different that normal login. So, I have created a different controller for Social login. Create a controller by running below command.

This will create a controller at directory app/Http/Controllers/Auth. Open it and modify. Final controller looks like below:

9Create Views

I will use bootstrap to beautify my design. I will first create a default.blade.php as a master template. Master template looks like below:

Now, we will modify our login blade for social login. So our final page looks like below:

That’s it. You have successfully integrated social login in your application. You can find more about the package in laravel official documentation.