Laravel 5.6 Socialite Facebook Login

In this tutorial, we’re going to explain how to integrate Facebook login in Laravel based application using Socialite. The Socialite is Laravel’s official package, which is making is easy to authenticate with external services with OAuth providers.

Create Project

Create a brand new Laravel 5.6 project with Composer create-project command:

Database connection settings

After creating project open .env file and update your database credentials:

Next step, would be, go to the project root and open the terminal and type the following command. It will create the two tables, which is by default ships by Laravel 5.6

You might get following error :

PDOException::(“SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes”)

to fix this, all you have to do is to edit your AppServiceProvider.php file and add to the boot method a default string length:

Lets add two new columns called provider_id, provder to the users table, create a migration file with below commnad

Open create migration file and update it as shown below

NOTE: Changing columns for table “users” requires Doctrine DBAL; so install “doctrine/dbal” with composer

Again run the php artisan migrate command from the project root. It will add new columns to the user table and it will modify email and password default values to null.

Now update your user model fillable array as shown below, with new columns

Installing Socialite

To get started with Socialite, use Composer to add the package to your project’s dependencies:

Log in to your Facebook developer account and create an application and grab the client id and client secret. Open your config/services.php configuration file and place credential inside it as shown below.
(In real app place config values in .env file, its a good practice).

so let’s define callback and redirect routes in routes/web.php file:

Create a controller called SocialAuthController with following artisan command

Now update your controller with below code:

Now update view file welcome.blade with below code:

That’s it, now start the application with php artisan serve, and access your application http://localhost:8000.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

Arjun
I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.
DMCA.com Protection Status