Laravel Facebook Login With Socialite Tutorial Example

Laravel Facebook Login With Socialite Tutorial Example is the topic of this article.

We will use a package called socialite to integrate facebook login in our laravel project.

Many times, user prefers to login with social accounts like facebook, google, twitter etc. instead of traditional email login for simplification.

Check the below video how facebook login in laravel takes place.

Step 1. New Project And Socialite

Make a new laravel project using the below command

  • It will make a new project called “larafb

Now we need to integrate socialite package into our newly created laravel project.

  • So fire up the below command in your terminal.

This command will take some time to download required files of socialite package which will help us to integrate facebook login seamlessly.

Step 2. Write providers and aliases

Adding provides

Once your terminal has integrated socialite package files, open config->app.php file.

  • Inside app.php file, you will find one array called ‘providers => []’
  • In this array, add the below line

Full source lines for ‘providers => []’ is something like this

You can see that I have added that line in above array at the middle of the array.

Inserting Aliases

There is another array like provides called Aliases in app.php file.

  • In Aliases array, add the below line at the end,

  • Full source code for Aliases array, is like the following

See at the last line, which we have inserted.

Step 3. App at Facebook Developer

Now we need to make a new app at facebook developer console.

  • Go to the :
  • Login to your account and you see the below screen.
  • laravel facebook login
    Click on My Apps and you will see a drop down as per the image.
  • Click on “Add New App” button. You will see the below screen.
  • laravel facebook login
    Enter app name in Display Name section and email in Contact Email section.
  • Then click on Create App ID button.

After this, at the left side panel, click on Settings->Basic tab. Now you will find the below screen.

laravel facebook login

Note down App ID and App Secret at some place. We will need this later.

  • In App Domains field, add localhost.
  • Scroll down till end and click on Add Platform. Below pop up will be there.

laravel facebook login
You can see that there are many platforms are available. You need to click on Website.

  • At the end of the screen, there is an option like “Site URL”
  • In this option, insert URL as “http://localhost:8000/”
  • At last, click on “Save Changes” button.

Step 4. Adding Facebook Credentials

Now we need to put App ID and App Secret in .env file. Open your .env file and add the below source lines

  • Replace your secret in the second line.

Now open your config->services.php file. Add below code in this file.

  • Above code will fetch the values of app id, secret and callback URL from the .env file.

Full source snippet for services.php file will be like this

Step 5. Changing Welcome Blade file

Open your resources->views->welcome.blade.php file.

  • We need to add below line in this file.

  • This line is a link, which will open facebook login process when the user clicks it.
  • I have added this line at just above the end of the file.

Whole code for welcome.blade.php file is as the following

Step 6. Routes and Authentication

Before adding any routes, first of all trigger the below command

  • It will add some files to enable authentication feature in your laravel app.
  • Now open routes->web.php file. Add the below source lines in this file.

  • First route is pointing to the redirect() method in SocialAuthController file.
  • Second is giving reference to the callback() method in SocialAuthController file.

So final code block for web.php looks like the below

Step 7. Making a Social Controller

In your terminal, fire the below command

This command will make a new controller named “SocialAuthController” inside app->Http->Controllers directory.

Add the below source snippet in this file

  • Above code includes two functions (redirect and callback) .
  • redirect() function will tell the compiler to load the facebook login screen.
  • When user give correct username and password of his facebook credentials, system will run callback() function.
  • $user will get the values like id, name, email etc. information from facebook.
  • Last line will redirect to the home.blade.php file. Here, we are passing $user and $service to home.blade.php file.

Step 8. Last View File

So now we have logged in with facebook and also have fetched some information like id, name etc. from it.

  • Let us display these information to the user.
  • Navigate to the resources->views->home.blade.php file.

Write down the below code snippet in this home.blade.php file.

  • System will load this file after successful login with facebook.

Look at the below code block

  • This code lines will check one if condition. If the value of $service is facebook then it will move inside if condition.
  • Then it will print name of the user. To get the name of the user, it will use $details.

Download Source Code For Laravel Facebook Login

I will take you to source code

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

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