How to Add Google Oauth Social Login With Laravel Socialite Jetstream

  • Install Jetstream (refer official documentation) composer require laravel/jetstream php artisan jetstream:install livewire

  • Install Socialite (you would need to install Jetstream before installing Socialite) composer require laravel/socialite

  • Create OAUth Screen & Credentials in Google Cloud Console

    Take care of the URLs that you enter in the Console and those that you provision in your app.

Add migration to update Users table. Add a field to store Google ID for the user.

php artisan make:migration add_google_id_to_users_table

$table->string('google_id')->nullable();

$table->dropColumn('google_id');

Update model - User

protected $fillable = [
    'google_id',
];

Create a controller to handle post OAuth login

php artisan make:controller SocialController

public function googleSocial() {
    $googleUser = Socialite::driver('google')->user();

    $newUser = User::updateOrCreate(
        [
            'google_id' => $googleUser->id,
        ],
        [
            'name' => $googleUser->name,
            'email' => $googleUser->email,
            'password' => Hash::make($googleUser->token),
        ]
    );
    
    Auth::login($newUser);

    return redirect('/dashboard');
}

Update route with the new SocialController

Route::get('/auth/redirect', function () {
    return Socialite::driver('google')->redirect();
});
    
Route::get('/auth/callback', [SocialController::class, 'googleSocial']);

Run migration

Extras

If you want to disable new-user registration (comment out the following lines in config\fortify.php)

// Features::registration(),
// Features::resetPasswords(),

Stylize Google login button

<div class="w-full">
    <a href="{{ url('auth/redirect') }}" class="block border-2 border-indigo-400 text-center my-4 p-2 rounded-md">
    <svg xmlns="https://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 48 48" aria-hidden="true" class="inline L5wZDc"><path fill="#4285F4" d="M45.12 24.5c0-1.56-.14-3.06-.4-4.5H24v8.51h11.84c-.51 2.75-2.06 5.08-4.39 6.64v5.52h7.11c4.16-3.83 6.56-9.47 6.56-16.17z"></path><path fill="#34A853" d="M24 46c5.94 0 10.92-1.97 14.56-5.33l-7.11-5.52c-1.97 1.32-4.49 2.1-7.45 2.1-5.73 0-10.58-3.87-12.31-9.07H4.34v5.7C7.96 41.07 15.4 46 24 46z"></path><path fill="#FBBC05" d="M11.69 28.18C11.25 26.86 11 25.45 11 24s.25-2.86.69-4.18v-5.7H4.34C2.85 17.09 2 20.45 2 24c0 3.55.85 6.91 2.34 9.88l7.35-5.7z"></path><path fill="#EA4335" d="M24 10.75c3.23 0 6.13 1.11 8.41 3.29l6.31-6.31C34.91 4.18 29.93 2 24 2 15.4 2 7.96 6.93 4.34 14.12l7.35 5.7c1.73-5.2 6.58-9.07 12.31-9.07z"></path><path fill="none" d="M2 2h44v44H2z"></path></svg>
    {{ __('Log in with Google') }}
    </a>
</div>