< go back to home

Laravel Authentication Series (Part 3): Facebook Register And Login Setup

May 18, 2020

This is the third part of a series of related articles regarding different Laravel authentication methods. We will show how to integrate facebook for login and registration of your website. You can check the setup here if you need a ready made Laravel to run in your local.

Start by creating a developer facebook account in https://developers.facebook.com/. Once you have an account and was able to login, click "Add New App" in the dashboard. Enter any name, then click "Create App ID".

Go to "Settings" > "Basic" in the left navigation bar. Take note of the "App ID" and "App Secret" as it will be used later in configuraiton of Laravel.

Next, go to "Settings" > "Advanced". In the "Share Redirect Whitelist", put your callback URL in the "Share Redirect Domain Whitelist". In this demo, it will be "http://localhost:8001/callback/facebook". This completes the parameters needed in Laravel application.

In the Laravel application, install "Socialite" library that we'll use for Facebook authentication.


Add the Facebook parameters in the ".env" file.


.env

Next is to make the parameters available in the configuration. Add the 'facebook' parameters in "config/services.php" file.


config/services.php

Create the register page. For this, we'll keep it as simple as we can. A page will display a link that would take you to Facebook login when clicked. In the "routes/web.php", add the register route.


resources/routes/web.php

As you see, the FB link will go to a route "redirect/facebook". Let's add that to the route including the callback URL "callback/facebook".


resources/routes/web.php

Generate the "FbController" file.


In the FbController file, place the codebase below. Check the inline comments for the explanation on what it does.


app/Http/Controllers/FbController.php