Setting up Laravel Socialite can produce some unwanted errors like: "Laravel\Socialite\Two\InvalidStateException" where is not clear how to fix it. In this article you can find out how to fix it.
So, you have setup laravel/socialite, call back is in the place, looks ok - but when callback comes Laravel throws an error. The fix to this problem is next:
- Go to config/session.php and find 'domain', it should look like this:
'domain' => env('SESSION_DOMAIN', null),
- Go to .env and set the SESSION_DOMAIN value to your domain:
SESSION_DOMAIN="${APP_URL}"
where APP_URLAPP_URL=http://localhost:8000
- Clear the cache and config settings cache, by using artisan commands:
php artisan cache:clear
php artisan config:clear
In some cases, when your traffic is coming from mobile phones where users use apps to login, you would have to attach stateless() to your Socialite call:
try {
$githubUser = Socialite::driver('github')->user();
} catch (InvalidStateException $e) {
$githubUser = Socialite::driver('github')->stateless()->user();
}
And that should be it, reload the page and try again - it should work.