LARAVEL

Using Facebook Authentication For Login in Laravel 5


Laravel has simplified authentication processes either the traditional way or via social media like Facebook, Twitter, LinkedIn or Github by creating a Laravel Socialite Package.

Let’s see how we can utilize this package for Facebook Authentication:

1. Do a clean install of Laravel 5

2. Install Socialite

3. Head over to your config/app.php and add the following in the providers array:

4. Still in your config/app.php file, also add this to the aliases array:

We just registered a Facade for Laravel Socialite.

5. Create Facebook app.

Head over to Facebook Developers Page . Under the My apps, there is a link to create a new app.

A dialog box should appear afterwards showing you different options, Choose Website since we are building a web app. So give your app a name and click “Create new Facebook App ID”.

I named mine “Laravel Auth”.

Another dialog box should appear. Choose the Category. I chose “Education” for the purpose of this tutorial.

 

Go ahead and create App ID.

Click on Facebook Login when you scroll down here:

 

6. Add Facebook Config

Head over to your config/services.php and add this:

Open your .env file and add your facebook client id, secret and callback url like so:

where xxxxxxx refers to your APP ID and APP secret. You can find those details when you click on the name of your app in the dropdown that “My apps” provides on the facebook developer dashboard page.

7. Set up Routes and Controller Methods

You will need two routes: one for redirecting the user to the OAuth provider, and another for receiving the callback from the provider after authentication.

Add these to routes.php

Let’s Open up our AuthController.php file in Auth folder and add this:

Note: Add the Socialite and Auth class reference at the top of this file like so:

 

Screen Shot 2015-08-24 at 6.12.30 AM

First, it redirects to Facebook, grabs the relevant information and then looks up the user and authenticate as that user. If the user doesn’t exist, it creates the user and authenticate.

8. Database

We need to update our users migration so that it will allow us to store facebook-specific information. Since this is a new app, I could just modify the users migration, but if you have an existing app, you’ll need to make a new migration.

9. Model

Make those fields mass assignable in your User model. name, email, facebook_id and avatar like so:

 

Now let’s get to work.

Head over to your welcome.php and edit it to have a login facebook button like so:

Don’t forget to link the file with bootstrap.

In your routes.php, add this:

Create home.blade.php

Copy the contents of welcome.blade.php into it and just update the body content to have this:

Now, run your migrations lest we forget like so:

Are you ready to test your app?..after all the work!

 

Now, run php artisan serve and click on the Login with Facebook button on your homepage

 

 

 

Yaaay!!!..Now we are logged in from facebook..and we can access our data from Facebook.

Note: I drew heavy inspiration from Matt Stauffer’s blog post.

So you can check how to use GitHub authentication for Login in Laravel 5 here

You can decide to use the data for anything,…this tutorial aims to simply show you how to authenticate with facebook.

You can find the full source code of this tutorial here

Please, if you have any questions or observations, let me know in the comments section!

PROSPER OTEMUYIWA

About PROSPER OTEMUYIWA

Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.