How to Login Facebook Account in Laravel using the Socialite Package

How to Login Facebook Account in Laravel using the Socialite Package

Learn how to login using Facebook social media account in Laravel using the Socialite package. Learn how to login with Facebook in Laravel 8 application. Laravel 8 Socialite package allows you to implement a robust, eloquent interface to OAuth authentication with various social media platforms such as Facebook, Twitter, Google, LinkedIn, GitHub, GitLab, and Bitbucket. It saves your time through its user-centric boilerplate social authentication mechanism.

How to login with Facebook in Laravel 8 application. If you have the same question, then get along; throughout this tutorial, we will learn how to login using Facebook social media account in Laravel using the Socialite package.

Laravel 8 Socialite package allows you to implement a robust, eloquent interface to OAuth authentication with various social media platforms such as Facebook, Twitter, Google, LinkedIn, GitHub, GitLab, and Bitbucket. It saves your time through its user-centric boilerplate social authentication mechanism.

Login with social accounts is a straightforward process and simultaneously enhances the user experience; nowadays, everybody knows a better user experience is the key to any digital product’s success.

Social login is a mechanism that propels the user’s expectation forward to access and register within the application. Consequently, we are creating a simple feature to log in with Facebook in the Laravel application. Authentication user interface templates are required for this tutorial, and we are using Laravel Jetstream to streamline the Authentication UI process.

Laravel Jetstream is a magnificently crafted authentication scaffolding designed using Tailwind CSS; moreover, you can create auth templates with Livewire or Inertia scaffolding. It offers login, registration, email verification, two-factor authentication, session management, API support for Laravel. Incredible, isn’t it?

Let’s start integrating Login with Facebook in the Laravel application.

Set Up Laravel Environment

Run composer command to install a new Laravel application:

composer create-project laravel/laravel --prefer-dist laravel-socialite-login-facebook-example

Enter inside the project:

cd laravel-socialite-login-facebook-example

Add Database Details

Go to the .env file, define the database name, user name, and password of your database.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=root
DB_PASSWORD=

Setting up Jetstream in Laravel

Install jetstream with following command:

composer require laravel/jetstream

Execute command to generate authentication templates such as login, register and email verification.

php artisan jetstream:install livewire

Next, run below command.

npm install

Run dev packages via node package manager.

npm run dev

Run command to migrate authentication properties.

php artisan migrate

Install Socialite Package in Laravel

Install socialite package in Laravel with the following command.

composer require laravel/socialite

Open config/app.php, register socialite plugin in providers, and aliases array.

....
....
'providers' => [
    ....
    ....
    Laravel\Socialite\SocialiteServiceProvider::class,
],

'aliases' => [
    ....
    ....
    'Socialite' => Laravel\Socialite\Facades\Socialite::class,
],
....
....

Add Facebook ID in Users Table

We need to relentlessly add the facebook id in users table.

php artisan make:migration add_fb_id_column_in_users_table --table=users

Head over to newly created file migrations/timestamp_add_fb_id_column_in_users_table.php file, add the fb_id column value.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddFbIdColumnInUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('fb_id')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('fb_id');
        });
    }
}

Also, add the Facebook ID table value in app/Models/User.php file.

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
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\Sanctum\HasApiTokens;

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

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

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
        'two_factor_recovery_codes',
        'two_factor_secret',
    ];

    /**
     * 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 = [
        'profile_photo_url',
    ];
}

laravel php web-development developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

PHP Web Application Development Company

As a top **PHP Web Application Development Company in USA**[https://www.dataeximit.com/php-development-services/](https://www.dataeximit.com/php-development-services/ "https://www.dataeximit.com/php-development-services/"), we at Data EximIT have...

PHP Website Development

Skenix Infotech is a leading PHP Web Development Company that helps companies get results with industry best practices. Get affordable PHP Development Services.

Laravel Development Company

Skenix Infotech is a top Laravel Website Development Company with Expert Laravel Developers that provides robust Laravel Development Services at fair costs.

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer](https://hourlydeveloper.io/hire-dedicated-php-developer/ "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** has always been an industry leader for companies and business owners looking to hire...