Laravel 8 Socialite Login with Facebook Account

Websolutionstuff | Mar-08-2021 | Categories : Laravel PHP

In this tutorial, we will learn laravel 8 socialite login with facebook account, as you all know currently many websites provide different types of login authentication facilities to users like facebook login, login with google, login with github, etc.

Here I will give you an example of a laravel 8 login with facebook account. Here I have used laravel 8 jetstream authentication for the laravel social login example.

I have added all details in this example with an image for easy implementation. So, let's start login with facebook with socialite step by step.

Step 1: Install Laravel 8 for socialite login with facebook account.

Step 2: Install JetStream for Authentication

Step 3: Install Socialite for laravel 8 login with facebook account

Step 4: Create Facebook App

Step 5: Changes in Config File

Step 6: Create Controller for facebook login

Step 7: Create Route

Step 8: Add Code in Blade File

Step 9: Add Column in Database

Step 10: Update the Model

Step 11: Run Project for output of laravel social login


Step 1: Install Laravel 8

In this step, we will install laravel 8 for this socialite login with facebook account.

composer create-project --prefer-dist laravel/laravel laravel_8_facebook_login


Step 2: Install JetStream

In this step, we will install jetstream. So, run the below command to install jetstream for laravel 8 jetstream login with facebook.

composer require laravel/jetstream

Now, we need to install livewire using the below artisan command and also we need basic authentication like login and registration otherwise you can install using auth command.

php artisan jetstream:install livewire

Now, Install Node and run the package.

npm install
npm run dev

 Now, we will create a database using the migration command.

php artisan migrate



Step 3: Install Socialite

 Now, we will install Socialite Package which provides API to connect with google accounts. So, run the below command.

composer require laravel/socialite


Step 4: Create Facebook App

In this step, we will create a facebook app using the developer account.



Now, Goto Facebook Settings and add your callback /redirect URL in Valid OAuth Redirect URL Save it as the below image.




Now, we required a Facebook client id and secret key, if you don't have a google app account then you need to create it from the below link. 

Create Facebook Account after creating an account you can copy the client id and secret key.




Step 5: Changes in Config File

Now we will set the app id, and secret key, and call back the URL in the config file so open this file config/services.php and add the id and secret key.

return [
    'facebook' => [
        'client_id' => 'your client ID',
        'client_secret' => 'your client secret',
        'redirect' => 'http://localhost:8000/callback',


Step 6: Create Controller

Now create Controller LoginWithFacebookController, in this controller we have added the redirect() function to redirect the user to Facebook, and the callback() function is used to handle the user when callback from Facebook.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Exception;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;

class LoginWithFacebookController extends Controller
    public function redirectFacebook()
        return Socialite::driver('facebook')->redirect();

    public function facebookCallback()
        try {
            $user = Socialite::driver('facebook')->user();
            $finduser = User::where('facebook_id', $user->id)->first();
                return redirect()->intended('dashboard');
                $newUser = User::create([
                    'name' => $user->name,
                    'email' => $user->email,
                    'facebook_id'=> $user->id,
                    'password' => encrypt('Test123456')
                return redirect()->intended('dashboard');
        } catch (Exception $e) {


Step 7: Create Route

Add the below code to your route file.



use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LoginWithFacebookController;

Route::get('/redirect', [LoginWithFacebookController::class, 'redirectFacebook']);
Route::get('/callback', [LoginWithFacebookController::class, 'facebookCallback']);


Step 8: Add Code in Blade File

 Now, add the below code in resources/views/auth/login.blade.php.

        <x-slot name="logo">
            <x-jet-authentication-card-logo />

        <x-jet-validation-errors class="mb-4" />

        @if (session('status'))
            <div class="mb-4 font-medium text-sm text-green-600">
                {{ session('status') }}

        <form method="POST" action="{{ route('login') }}">

                <x-jet-label for="email" value="{{ __('Email') }}" />
                <x-jet-input id="email" class="block mt-1 w-full" type="email" name="email" :value="old('email')" required autofocus />

            <div class="mt-4">
                <x-jet-label for="password" value="{{ __('Password') }}" />
                <x-jet-input id="password" class="block mt-1 w-full" type="password" name="password" required autocomplete="current-password" />

            <div class="block mt-4">
                <label for="remember_me" class="flex items-center">
                    <input id="remember_me" type="checkbox" class="form-checkbox" name="remember">
                    <span class="ml-2 text-sm text-gray-600">{{ __('Remember me') }}</span>

            <div class="flex items-center justify-end mt-4">
                @if (Route::has('password.request'))
                    <a class="underline text-sm text-gray-600 hover:text-gray-900" href="{{ route('password.request') }}">
                        {{ __('Forgot your password?') }}

                <x-jet-button class="ml-4">
                    {{ __('Login') }}
            <div class="flex items-center justify-end mt-4">
                <a class="ml-1 btn btn-primary" href="{{ url('redirect') }}" style="margin-top: 0px !important;background: #4c6ef5;color: #ffffff;padding: 5px;border-radius:7px;" id="btn-fblogin">
                    <i class="fa fa-facebook-square" aria-hidden="true"></i> Login with Facebook



Step 9: Add a Column in the Database

In this step, add a column in the user table and set the name as facebook_id.

php artisan make:migration Add_Facebook_Id_To_Users

Now, add a new column in your code like below.

 public function up()
        Schema::table('users', function (Blueprint $table) {


Step 10: Update the Model

Now, update the model as below.


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Jetstream\HasTeams;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
    use HasApiTokens;
    use HasFactory;
    use HasProfilePhoto;
    use HasTeams;
    use Notifiable;
    use TwoFactorAuthenticatable;

     * The attributes that are mass assignable.
     * @var array
    protected $fillable = [
        'name', 'email', 'password','facebook_id',

     * The attributes that should be hidden for arrays.
     * @var array
    protected $hidden = [

     * The attributes that should be cast to native types.
     * @var array
    protected $casts = [
        'email_verified_at' => 'datetime',

     * The accessors to append to the model's array form.
     * @var array
    protected $appends = [




You might also like:

Recommended Post
Featured Post
How to Send Bulk Mail Using Queue in Laravel 8
How to Send Bulk Mail Using Qu...

In this article, we will see how to send bulk mail using a queue in laravel 8. Laravel queue is used for sending bu...

Read More


Bootstrap Session Timeout Example In Laravel
Bootstrap Session Timeout Exam...

In this tutorial, I will show you a session timeout example in laravel. After a set amount of idle time, the bootstrap w...

Read More


How To Integrate Stripe Payment Gateway In Laravel 9
How To Integrate Stripe Paymen...

In this article, we will see how to integrate the stripe payment gateway in laravel 9. stripe payment gateway is in...

Read More


How to Add Active Class Dynamically in Laravel 11
How to Add Active Class Dynami...

Hello, laravel web developers! In this article, we'll see how to add an active class dynamically in laravel 11. Here...

Read More