Facebook OAuth Login in Laravel 12 Using Socialite

Facebook OAuth Login in Laravel 12 Using Socialite

In this tutorial, you will learn how to implement Facebook OAuth Login in Laravel 12 using Laravel Socialite, following a clean, scalable, and production-ready architecture that supports multiple OAuth providers.

🔥 What You Will Build

By the end of this tutorial, you will have:

  • ✅ Facebook OAuth authentication

  • ✅ Automatic user creation

  • ✅ Facebook profile data storage

  • ✅ Secure authentication flow

  • ✅ Production-ready database structure

  • ✅ Extendable setup (Google, GitHub, GitLab, etc.)

🧰 Requirements

Ensure the following are installed on your system:

  • PHP 8.2+

  • Laravel 12

  • MySQL / MariaDB

  • Composer

  • Facebook Developer Account

Step 1: Create Facebook OAuth Credentials

1.1 Facebook Developer Setup

  1. Visit 👉 https://developers.facebook.com/

  2. Click My Apps

  3. Click Create App

  4. Select Consumer

  5. App Name: Laravel Facebook Login

  6. Click Create App

1.2 Add Facebook Login Product

Inside your app dashboard:

  1. Click Add Product

  2. Select Facebook Login

  3. Choose Web

Configure URLs:

FieldValue
Site URLhttp://localhost:8000
Valid OAuth Redirect URIshttps://localhost:8000/auth/facebook/callback

1.3 Get Client Credentials

From Settings → Basic, copy:

  • App ID

  • App Secret

⚠️ Never expose your App Secret publicly

Step 2: Install Laravel Socialite

composer require laravel/socialite

Laravel 12 supports auto-discovery, so no additional setup is required.

Step 3: Configure Facebook Service

📄 config/services.php

'facebook' => [ 'client_id' => env('FACEBOOK_CLIENT_ID'), 'client_secret' => env('FACEBOOK_CLIENT_SECRET'), 'redirect' => env('FACEBOOK_REDIRECT_URI'), ],

Step 4: Add Environment Variables

📄 .env

FACEBOOK_CLIENT_ID=your_facebook_app_id FACEBOOK_CLIENT_SECRET=your_facebook_app_secret FACEBOOK_REDIRECT_URI=http://localhost:8000/auth/facebook/callback

Step 5: Users Table (Multi-Provider Ready)

Your users table already supports OAuth providers.

$table->string('provider_id')->nullable()->unique(); $table->string('provider')->nullable(); // facebook, google, github

Run migrations if needed:

php artisan migrate

Step 6: Create Facebook Authentication Controller

php artisan make:controller Auth/FacebookAuthController

📄 app/Http/Controllers/Auth/FacebookAuthController.php

<?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; use Illuminate\Support\Str; class FacebookAuthController extends Controller { public function redirect() { return Socialite::driver('facebook') ->scopes(['email', 'public_profile']) ->redirect(); } public function callback() { try { $facebookUser = Socialite::driver('facebook')->stateless()->user(); $email = $facebookUser->email ?? $facebookUser->id . '@facebook-user.local'; $user = User::where('provider_id', $facebookUser->id) ->orWhere('email', $email) ->first(); if (!$user) { $user = User::create([ 'name' => $facebookUser->name ?? 'Facebook User', 'email' => $email, 'provider_id' => $facebookUser->id, 'provider' => 'facebook', 'avatar' => $facebookUser->avatar, 'password' => bcrypt(Str::random(24)), 'role_name' => 'User', 'status' => 'Active', 'join_date' => now(), ]); } $user->update([ 'last_login' => now(), ]); Auth::login($user); return redirect()->route('home'); } catch (\Exception $e) { \Log::error('Facebook OAuth Error', [ 'message' => $e->getMessage(), ]); return redirect()->route('login') ->withErrors('Facebook authentication failed.'); } } }

Step 7: Define Routes

📄 routes/web.php

use App\Http\Controllers\Auth\FacebookAuthController; Route::get('/auth/facebook', [FacebookAuthController::class, 'redirect']) ->name('facebook.login'); Route::get('/auth/facebook/callback', [FacebookAuthController::class, 'callback']);

Step 8: Add Facebook Login Button

📄 resources/views/auth/login.blade.php

<a href="{{ route('facebook.login') }}" class="btn btn-primary w-100"> <i class="bi bi-facebook"></i> Login with Facebook </a>

Step 9: Protect Dashboard Route

Route::get('/dashboard', function () { return view('dashboard'); })->middleware('auth')->name('home');

Step 10: Run & Test

php artisan serve

✅ Test Flow

  1. Open the login page

  2. Click Login with Facebook

  3. Authorize the app

  4. Redirected to dashboard 🎉

🔐 Security Best Practices

  • Use HTTPS in production

  • Handle missing Facebook emails

  • Limit OAuth scopes

  • Auto-assign user roles

  • Enable account linking

  • Log authentication failures

  • Verify Facebook app before production

🎯 Final Result

  • ✅ Facebook OAuth fully integrated

  • ✅ Automatic user creation

  • ✅ Secure authentication flow

  • ✅ Multi-provider ready

  • ✅ Laravel 12 compatible

  • ✅ Production-ready structure

📥 Full Source Code

👉 GitHub Repository
https://github.com/StarCodeKh/Login-with-Laravel-12-Using-Socialite

Souy Soeng

Souy Soeng

Hi there 👋, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
👯 I’m looking to collaborate on open-source PHP & JavaScript projects
💬 Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
close