GitHub Login in Laravel 12 | Passwordless Authentication with Socialite

GitHub Login in Laravel 12 | Passwordless Authentication with Socialite

GitHub Login allows users to authenticate quickly using their GitHub account without creating a traditional password.

In this tutorial, you’ll learn how to implement GitHub OAuth Login in Laravel 12 using Laravel Socialite with a clean, scalable, and production-ready database structure.

🔥 What You Will Build

By the end of this tutorial, you will have:

  • ✅ GitHub OAuth Login

  • ✅ Automatic user creation

  • ✅ GitHub profile data storage

  • ✅ Secure authentication flow

  • ✅ Production-ready structure

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

🧰 Requirements

Make sure you have the following installed:

  • PHP 8.2+

  • Laravel 12

  • MySQL / MariaDB

  • Composer

  • GitHub Account

Step 1: Create GitHub OAuth Credentials

1.1 GitHub Developer Settings

Visit GitHub Developer Settings:

👉 https://github.com/settings/developers

  1. Click OAuth Apps

  2. Click New OAuth App

  3. Fill in the required fields:

FieldValue
Application nameLaravel GitHub Login
Homepage URLhttp://127.0.0.1:8000
Authorization callback URLhttp://127.0.0.1:8000/auth/github/callback
  1. Click Register application

1.2 Get Client Credentials

After creating the app, copy:

  • Client ID

  • Client Secret

⚠️ Keep your Client Secret private!

Step 2: Install Laravel Socialite

Run the following command:

composer require laravel/socialite

Laravel 12 supports package auto-discovery, so no manual provider registration is required.

Step 3: Configure GitHub Service

Open the services configuration file:

📄 config/services.php

'github' => [ 'client_id' => env('GITHUB_CLIENT_ID'), 'client_secret' => env('GITHUB_CLIENT_SECRET'), 'redirect' => env('GITHUB_REDIRECT_URI'), ],

Step 4: Add Environment Variables

Open your .env file and add:

GITHUB_CLIENT_ID=your_github_client_id GITHUB_CLIENT_SECRET=your_github_client_secret GITHUB_REDIRECT_URI=http://127.0.0.1:8000/auth/github/callback

Step 5: Create GitHub-Ready Users Table

Update your users table migration:

📄 database/migrations/create_users_table.php

Schema::create('users', function (Blueprint $table) { $table->id(); // Basic Info $table->string('name')->nullable(); $table->string('user_id')->nullable(); $table->string('email')->unique()->nullable(); // Social Login $table->string('provider_id')->nullable()->unique(); // OAuth user ID $table->string('provider')->nullable(); // github, google, facebook
// User Metadata $table->string('join_date')->nullable(); $table->string('last_login')->nullable(); $table->string('phone_number')->nullable(); $table->string('status')->nullable(); $table->string('role_name')->nullable(); $table->string('avatar')->nullable(); $table->string('position')->nullable(); $table->string('department')->nullable(); $table->string('line_manager')->nullable(); $table->string('seconde_line_manager')->nullable(); // Authentication $table->timestamp('email_verified_at')->nullable(); $table->string('password')->nullable(); $table->rememberToken(); $table->timestamps(); });

Run the migration:

php artisan migrate

Step 6: Create GitHub Authentication Controller

Generate the controller:

php artisan make:controller Auth/GithubAuthController

📄 app/Http/Controllers/Auth/GithubAuthController.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 GithubAuthController extends Controller { public function redirect() { return Socialite::driver('github')->redirect(); } public function callback() { try { $githubUser = Socialite::driver('github')->stateless()->user(); $user = User::where('provider_id', $githubUser->id) ->orWhere('email', $githubUser->email) ->first(); if (!$user) { $user = User::create([ 'name' => $githubUser->name ?? $githubUser->nickname, 'email' => $githubUser->email, 'provider_id' => $githubUser->id, 'provider' => 'github', 'avatar' => $githubUser->avatar, 'password' => bcrypt(Str::random(24)), 'role_name' => 'User', 'status' => 'Active', 'join_date' => now(), ]); } else { if (!$user->github_id) { $user->update([ 'github_id' => $githubUser->id, 'provider' => 'github', ]); } } $user->update([ 'last_login' => now(), ]); Auth::login($user); return redirect()->route('home'); } catch (\Exception $e) { \Log::error('GitHub OAuth Error', [ 'message' => $e->getMessage(), ]); return redirect()->route('login') ->withErrors('GitHub authentication failed.'); } } }

Step 7: Define Routes

📄 routes/web.php

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

Step 8: Add GitHub Login Button

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

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

Step 9: Protect Dashboard Route

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

Step 10: Run & Test the Application

php artisan serve

✅ Test Flow

  1. Open login page

  2. Click Login with GitHub

  3. Authorize GitHub access

  4. Redirected to dashboard 🎉

🔐 Security Best Practices

  • Use HTTPS in production

  • Handle missing GitHub emails (private emails)

  • Limit OAuth scopes

  • Auto-assign roles

  • Enable account linking

  • Log authentication failures

🎯 Final Result

  • GitHub OAuth fully integrated

  • Automatic user creation

  • Secure login flow

  • Clean & scalable database

  • Laravel 12 compatible

  • Production-ready

📥 Download 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