I had Google authentication working correctly and suddenly it started giving me the following error in Production (On localhost everything continues to work normally). I searched a lot before making this query and I tried all the options on the web and I can't find the solution.
The bug: Debug
Laravel\Socialite\Two\InvalidStateException
And in LoginController I get an error in the following line:
$userSocialite = Socialite::driver($driver)->user();
In the server log:
local.ERROR: {"exception":"[object] (Laravel\Socialite\Two\InvalidStateException(code: 0): at /home/brooklyn/System/vendor/laravel/socialite/src/Two/AbstractProvider.php:209 ) [stacktrace]
My LoginController:
//Socialite
public function redirectToProvider($driver)
{
$drivers = ['google'];
if(in_array($driver, $drivers)){
return Socialite::driver($driver)->redirect();
}else{
return redirect()->route('login')->with('info', $driver . ' no es una aplicación valida para poder loguearse');
}
}
public function handleProviderCallback(Request $request, $driver)
{
if($request->get('error')){
return redirect()->route('login')->with('info', 'Ocurrió un error, vuelva a intentarlo más tarde.');
}
$userSocialite = Socialite::driver($driver)->user();
$social_profile = SocialProfile::where('social_id', $userSocialite->getId())
->where('social_name', $driver)->first();
if(!$social_profile){
$user = User::where('email', $userSocialite->getEmail())->first();
if(!$user){
$user = User::create([
'name' => $userSocialite->user['given_name'],
'apellido' => $userSocialite->user['family_name'],
'email' => $userSocialite->getEmail(),
'permiso' => 'USUARIO',
'tipo_usuario' => 'PARTICULAR'
]);
}
SocialProfile::create([
'user_id' => $user->id,
'social_id' => $userSocialite->getId(),
'social_name' => $driver,
'social_avatar' => $userSocialite->getAvatar()
]);
}
auth()->login($social_profile->user);
return redirect()->route('panel');
/* dd($user->user['given_name']); */
// $user->token;
}
TESTS CARRIED OUT WITH NO EFFECT
1 TEST
On Localhost if I make the following modification:
In session.php
I change this:
'domain' => env('SESSION_DOMAIN', null),
A:
'domain' => env('SESSION_DOMAIN', 'midominio.com'),
On Local I get the same error as production. And in production the error persists.
2 TEST
Add to LoginController
Socialite::driver('google')->stateless()->user();
I get the following error:
GuzzleHttp\Exception\ClientException Client error:
POST https://www.googleapis.com/oauth2/v4/token
resulted in a400 Bad Request
response: { "error": "invalid_request", "error_description": "Missing required parameter: code" }
NEW TESTS
Create with another gmail account all the Auth and the error persists.
Also delete the whole project on the server and create a new account and it didn't help.
After several days struggling with this I was able to fix it by adding the following code:
I don't know if it's the best solution, but it's the only one I found.
Also in my case I had a problem in the following part: detected by @L.Flor, Thanks