Providing a seamless authentication system is even more important in today’s web applications. Users want easy and quick ways to get to your application without remembering yet another password. This is where a feature like Login with GitHub in Laravel becomes a real game-changer for developers and users alike. Consider the situation where you are developing a tool for developers or a SaaS solution of any kind. Your target group of people is already spending several hours a day on GitHub every day. Why not allow them to log in with a click? The Laravel 12 ecosystem has made it very simple for you with the help of the Socialite package.
Through this tutorial, you will be shown how to implement the GitHub login process for the Laravel 12 environment step by step. We will start from the beginning and cover the process of setting up your GitHub OAuth app and writing manageable code for a safe and efficient authentication process that takes mere seconds for your users to complete.
What is Laravel Socialite?
Laravel Socialite provides an elegant and simple way to authenticate users using OAuth providers. It handles all the complex OAuth flow behind the scenes so you can focus on building your application.
Socialite supports popular platforms like Facebook, Google, Twitter, LinkedIn, and of course, GitHub. The package abstracts away the headache of token management, redirects, and user data retrieval. You write minimal code and get maximum functionality.
Installing Laravel Socialite
Getting Socialite into your Laravel 12 project takes just one command. Open your terminal and run:
composer require laravel/socialite
Laravel automatically discovers the package, so no manual service provider registration is needed. You are ready to configure your OAuth credentials right away.
Benefits of Using Socialite for GitHub Authentication
- Reduces user friction by eliminating the need for password creation
- Increases conversion rates as users prefer one-click sign-ups
- Enhances security by leveraging GitHub’s robust authentication system
- Saves development time with pre-built OAuth implementation
- Provides verified user email addresses directly from GitHub
- Simplifies codebase maintenance with clean and readable syntax
Creating a GitHub OAuth Application for GitHub Login
Before writing any code, you need to register your application on GitHub. This process generates the credentials your Laravel application needs to communicate securely with GitHub.
Log into your GitHub account and navigate to Settings. Scroll down to Developer settings in the left sidebar. Click on OAuth Apps and then hit the New OAuth App button.
Create new app and complete the registration form with the following information:
- Application name: Enter your project name (e.g., My Laravel App)
- Homepage URL: Your application URL (e.g.,
http://localhost:8000) - Authorization callback URL:
http://localhost:8000/auth/github/callback
Click Register application to generate your credentials. GitHub will display your Client ID immediately. Click Generate a new client secret to create your secret key. Copy both values and keep them safe. You will need these in the next section.
Implementing Laravel 12 Socialite GitHub Authentication
Now comes the technical part. Let us wire everything together and make Laravel 12 social login with GitHub work smoothly.
Configure Environment Variables
Open your .env file and add your GitHub credentials:
GITHUB_CLIENT_ID=your_client_id_here
GITHUB_CLIENT_SECRET=your_client_secret_here
GITHUB_REDIRECT_URL=http://localhost:8000/auth/github/callback
Open config/services.php and add the GitHub configuration array:
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_REDIRECT_URL'),
],
Update the Users Model and Migration
Your users table needs a column to store the GitHub ID. Create a new migration:
php artisan make:migration add_github_id_to_users_table --table=users
Update the migration file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('github_id')->nullable()->after('id');
});
}
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('github_id');
});
}
};
Once migration changes are done, you need to run migration command to apply changes to database.
php artisan migrate
Open app/Models/User.php and add github_id to the fillable array:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, Notifiable;
protected $fillable = [
'name',
'email',
'password',
'github_id',
];
protected $hidden = [
'password',
'remember_token',
];
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
}
Create the GitHub Authentication Controller
Let’s generate a dedicated controller for handling GitHub authentication process. so we can separate or integrate multiple authentication like Facebook, google, twitter in future and it does not effect code organization.
php artisan make:controller Auth/GitHubController
Add the following code to app/Http/Controllers/Auth/GitHubController.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;
class GitHubController extends Controller
{
public function redirect()
{
return Socialite::driver('github')->redirect();
}
public function callback()
{
$githubUser = Socialite::driver('github')->user();
$user = User::updateOrCreate(
['github_id' => $githubUser->getId()],
[
'name' => $githubUser->getName() ?? $githubUser->getNickname(),
'email' => $githubUser->getEmail(),
'github_id' => $githubUser->getId(),
'password' => bcrypt(str()->random(16)),
]
);
Auth::login($user);
return redirect()->intended('/dashboard');
}
}
This controller does two important methods. The redirect method sends users to GitHub for authorisation. The callback method handles the response and creates or updates the user record.
Define the Routes
Open routes/web.php and add the authentication routes:
<?php
use App\Http\Controllers\Auth\GitHubController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/auth/github', [GitHubController::class, 'redirect'])
->name('auth.github');
Route::get('/auth/github/callback', [GitHubController::class, 'callback'])
->name('auth.github.callback');
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware('auth')->name('dashboard');
Add the Login Button
Create a simple login button in your view file. Add this to your welcome or login page:
<a href="{{ route('auth.github') }}" class="github-login-btn">
Login with GitHub
</a>
Style it however you prefer. Most developers use GitHub’s brand colours (black background with white text) for a familiar look.
Test Your Implementation
Test Your Implementation
php artisan serve
Visit http://localhost:8000 and click your GitHub login button. GitHub will ask for authorisation. After approval, you will land on your dashboard as an authenticated user.
Conclusion
You have successfully learned how to authenticate GitHub accounts using Laravel 12 with Socialite. This was achieved by following a few simple steps that required minimal code. Your users can now have a seamless login experience that saves them time and eliminates password fatigue.
With this GitHub OAuth integration built for Laravel 12, the future is bright for developing developer-engaging applications. Your users trust GitHub with their code. Now they can trust your application with the same credentials.
