Check our tools small Tools Laravel 11 Socialite Login with Github Account Example
Laravel 11 Socialite Login with Github Account Example

Laravel 11 Socialite Login with Github Account Example

In this tutorial, we will learn how to integrate Github login using Socialite in a Laravel 11 application. Socialite provides an easy way to handle OAuth authentication and is well-suited for integrating social logins like Github.

Prerequisites

  • Laravel 11 installed
  • Composer installed
  • Github account

Step 1: Install Laravel

First, install Laravel if you haven’t already. You can create a new Laravel project by running:

composer create-project --prefer-dist laravel/laravel laravel-socialite
cd laravel-socialite

Step 2: Install Socialite

Next, install Socialite using Composer. Run the following command in your project directory:

composer require laravel/socialite

Step 3: Configure Github App

  1. Go to Github Developer Settings and create a new OAuth App.
  2. Fill in the application details. For the Authorization callback URL, use:
   http://your-app-url/auth/github/callback
  1. After creating the app, you will get a Client ID and Client Secret.

Step 4: Update Environment File

Open your .env file and add the Github client ID, client secret, and redirect URL:

GITHUB_CLIENT_ID=your_github_client_id
GITHUB_CLIENT_SECRET=your_github_client_secret
GITHUB_REDIRECT_URL=http://your-app-url/auth/github/callback

Step 5: Configure Socialite

In your config/services.php file, add the Github configuration:

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

Step 6: Create Routes

Open routes/web.php and add routes for Github login:

use App\Http\Controllers\SocialiteController;

Route::get('auth/github', [SocialiteController::class, 'redirectToGithub']);
Route::get('auth/github/callback', [SocialiteController::class, 'handleGithubCallback']);

Step 7: Create Controller

Next, create a SocialiteController to handle the redirection and callback:

php artisan make:controller SocialiteController

Then, open app/Http/Controllers/SocialiteController.php and add the following code:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

class SocialiteController extends Controller
{
    public function redirectToGithub()
    {
        return Socialite::driver('github')->redirect();
    }

    public function handleGithubCallback()
    {
        $githubUser = Socialite::driver('github')->user();

        // Find or create the user
        $user = User::firstOrCreate(
            ['email' => $githubUser->getEmail()],
            ['name' => $githubUser->getName(), 'github_id' => $githubUser->getId()]
        );

        // Log the user in
        Auth::login($user, true);

        // Redirect to the desired page
        return redirect()->intended('dashboard');
    }
}

Step 8: Update User Model

In your app/Models/User.php, add the github_id column to the $fillable array:

protected $fillable = [
    'name',
    'email',
    'password',
    'github_id',
];

Step 9: Run Migrations

To store the Github ID, we need to add a new column to the users table. Create a new migration:

php artisan make:migration add_github_id_to_users_table --table=users

In the migration file, add the new column:

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->string('github_id')->nullable();
    });
}

public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropColumn('github_id');
    });
}

Run the migration:

php artisan migrate

Step 10: Create a View for Login

Create a simple login view resources/views/auth/login.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>Login with Github</title>
</head>
<body>
    <a href="{{ url('auth/github') }}">Login with Github</a>
</body>
</html>

Step 11: Testing

Now, navigate to http://your-app-url/auth/github to test the Github login. You should be redirected to Github for authentication and then back to your application.

Conclusion

You have successfully integrated Github login into your Laravel 11 application using Socialite. This approach can be easily extended to other social login providers supported by Socialite.