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.

Published by 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.

Join the Conversation

3 Comments

  1. Facebook https problem. Hi Arjun! I’m having a problem with the tutorial and it is because Facebook block every request made from a website without a https protocol. Laravel serves every project in a http protocol so, how can i test this app withouth being able to serve my project with a https protocol ?

    1. Install self signed certificate, if you are XAMPP user do following:
      To fix the SSL certificate error message “SSL certificate error: unable to get local issuer certificate” try this:

      Download: http://curl.haxx.se/ca/cacert.pem
      Copy the file cacert.pem into the directory: C:xamppphpextrasssl
      Open c:xamppphpphp.ini with Notepad++
      Search for the [curl] section
      Add the following lines:
      [curl]
      ; A default value for the CURLOPT_CAINFO option. This is required to be an
      ; absolute path.
      curl.cainfo=”C:xamppphpextrassslcacert.pem”
      openssl.cafile=”C:xamppphpextrassslcacert.pem”
      Stop / start Apache

Leave a comment

Your email address will not be published. Required fields are marked *