Go to Home

Facebook Login Integration in Laravel 6

In this article I'm going to walk you through Facebook Login integration via Laravel's Socialite package. This app is completely going to be local project and we are going to serve the application in https using the valet secure since facebook will not accept if the website isn't secure. Let's get started by creating a new fresh laravel project

laravel new social

Now, secure the project by running valet secure

valet secure social

We'll use the laravel's inbuilt authentication system for our project. So run the following

composer require laravel/ui

laravel/ui ships with three variants such as vue, react and basic bootstrap one. We'll use the basic bootstrap version.

php artisan ui bootstrap

Now we need to run the npm commands to set up the base

npm install && npm run dev

After running the npm modules, run the following comand to publish the authentication scaffolding

php artisan ui:auth

If everything goes fine until now, you'll be seeing laravel's default homepage as follows

Laravel Socialite

Lets require the laravel socialite package for our project

composer require laravel/socialite

Database

Instead of maintaining a different columns for multiple social accounts which you might need in the future, lets put all the social link information on the same column named "social" and we'll make it as json column

Please note that in the above code I have made the password field as nullable. This is because when users are doing social login, user will not be entering their passwords.

We'll create the database named social and we'll provide the details on .env file.

Now, lets clear the cached config and migrate the tables

php artisan config:cache
php artisan migrate

Since we have made our social column as json, we have to define getters in our User model so that we will get social result as object instead of string

Let's create SocialController inside Auth folder

php artisan make:controller Auth/SocialController

Then, we'll make changes to our web.php file to handle the facebook login

Here please note that I have used dynamic {provider} instead of static facebook. This is because, incase if you plan to add more social login options, then you need to write code again for that.

Facebook configuration

Head over to developers.facebook.com after logging in facebook and create your first app Facebook Developers Page Create App

Please select the Facebook login below

Select Facebook Login

Select the www below

Select WWW

Enter our project's base url below

Fill website details

Now click on Basic option under the Settings in left sidebar and fill the following details

  • Namespace - should be unique name
  • App Domains - your project's base url
Fill necessary details

Finally, add the redirect url under the Facebook Login > Settings on the left sidebar

Redirect URL

I prefer maintaining all the facebook social login details like FACEBOOK_CLIENT_ID, FACEBOOK_CLIENT_SECRET and FACEBOOK_REDIRECT_URL configuration in the .env file rather than the configuration file directly.

Now, lets add the facebook configuration to app/config/services.php file like the below

View

Finally, lets add the facebook login link mentioned above to the following pages.

  • auth/login.blade.php
  • auth/register.blade.php

Thats all you have to do to integrate Facebook login in Laravel 6. Hope you find this useful.

Happy Coding