Laravel Google Login invalid_grant

93 Views Asked by At

I'm working on my Laravel project and I have already configured registration and login with Google through Laravel Socialite. Everything works correctly from the browser, the problem comes when I use the PWA. On some devices, when you log in with Google, instead of opening within the PWA itself, the opening is forced in the device's browser (Google Chrome for example), when choosing the account the redirection is done and you return to the PWA but I get the error:

Client error:

POST https://www.googleapis.com/oauth2/v4/token resulted in a 400 Bad Request response: { "error": "invalid_grant", "error_description": "Bad Request" }

This only happens on devices where the PWA forces the opening of an external browser, for example, in the PWA of a PC where everything opens within itself, I don't have the problem. Similarly, if I go through the entire process from the browser itself, I also don't get the error.

My code is:

Route::get('/google-auth/callback', function () {
    $user_google = Socialite::driver('google')->stateless()->user();

    $name = $user_google->user['given_name'] ?? 'Name';
    $lastname = $user_google->user['family_name'] ?? 'Lastname';

    $user = User::updateOrCreate([
        'google_id' => $user_google->id,
    ], [
        'name' => $name,
        'lastname' => $lastname,
        'email' => $user_google->email,
        'email_verified_at' => now(),
    ]);

    Auth::login($user);
    return redirect('/');
});

This is the image of the error I get:

This is the image of the error I get

0

There are 0 best solutions below