This tutorial will guide you through the process of adding Facebook login functionality to your Laravel application.
Prerequisites
- Laravel project set up
- Composer installed
- Facebook Developer Account
Step 1: Create a Facebook App
- Go to Facebook Developers
- Click on "My Apps" and then "Create App"
- Choose "Consumer" as the app type
- Fill in the app details and create the app
- In the app dashboard, note down your App ID and App Secret
Step 2: Install Laravel Socialite
Laravel Socialite provides an expressive, fluent interface to OAuth authentication with Facebook, Twitter, Google, LinkedIn, GitHub, GitLab and Bitbucket.
Install it via Composer:
composer require laravel/socialite
Step 3: Configure Socialite
Add the following to your config/services.php file:
'facebook' => [ 'client_id' => env('FACEBOOK_CLIENT_ID'), 'client_secret' => env('FACEBOOK_CLIENT_SECRET'), 'redirect' => env('FACEBOOK_REDIRECT_URI'), ],
Then, add these to your .env file:
FACEBOOK_CLIENT_ID=your_facebook_app_id FACEBOOK_CLIENT_SECRET=your_facebook_app_secret FACEBOOK_REDIRECT_URI=http://localhost:8000/login/facebook/callback
Step 4: Set up Routes
Add these routes to your routes/web.php:
use App\Http\Controllers\Auth\FacebookController; Route::get('login/facebook', [FacebookController::class, 'redirectToFacebook'])->name('login.facebook'); Route::get('login/facebook/callback', [FacebookController::class, 'handleFacebookCallback']);
Step 5: Create FacebookController
Create a new controller:
php artisan make:controller Auth/FacebookController
Implement the controller:
<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use App\Models\User; use Illuminate\Support\Facades\Auth; use Laravel\Socialite\Facades\Socialite; class FacebookController extends Controller { public function redirectToFacebook() { return Socialite::driver('facebook')->redirect(); } public function handleFacebookCallback() { try { $user = Socialite::driver('facebook')->user(); $finduser = User::where('facebook_id', $user->id)->first(); if ($finduser) { Auth::login($finduser); return redirect()->intended('dashboard'); } else { $newUser = User::create([ 'name' => $user->name, 'email' => $user->email, 'facebook_id'=> $user->id, 'password' => encrypt('123456dummy') ]); Auth::login($newUser); return redirect()->intended('dashboard'); } } catch (\Exception $e) { dd($e->getMessage()); } } }
Step 6: Update User Model
Add facebook_id to the fillable array in your User model:
protected $fillable = [ 'name', 'email', 'password', 'facebook_id', ];
Step 7: Add Facebook ID to Users Table
Create a new migration:
php artisan make:migration add_facebook_id_to_users_table
In the new migration file:
public function up() { Schema::table('users', function ($table) { $table->string('facebook_id')->nullable(); }); } public function down() { Schema::table('users', function ($table) { $table->dropColumn('facebook_id'); }); }
Run the migration:
php artisan migrate
Step 8: Add Login Button
In your login view, add a "Login with Facebook" button:
<a href="%7B%7B%20route('login.facebook')%20%7D%7D" class="btn btn-primary"> Login with Facebook </a>
The above is the detailed content of How to Implement \'Login with Facebook\' in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools
