I\'m using Laravel Socialite to add a Facebook connect button on a website. Sometimes, I\'ve got this error on callback:
exception \'Laravel\\Socialite\\Two\
I had a similar issue, I've got
InvalidStateException in AbstractProvider.php line 182
in the function handleProviderCallback()
when it re-directs back from Facebook login. It seems to be the same as your issue.
Furthermore I found my issue occurs when I open my site without www
. When I open my site with www.mysite.com
- no problem. At first I think my issue is random until I've got the clue by Chris Townsend's reply to the question.
The Solution
Go to your www root, check the laravel file config/session.php
. Check session Session Cookie Domain
. The default configuration is
'domain' => null,
I made a change to
'domain' => 'mysite.com'
After php artisan cache:clear
and composer dump-autoload
, I can login with no issue from both www.mysite.com and mysite.com
Be sure to delete your cookies from browser when testing it after these modifications are done. Old cookies can still produce problems.
this solved it for me
$request->session()->put('state',Str::random(40));
$user = Socialite::driver('github')->stateless()->user();
For me the solution was to set APP_URL
to http://localhost:8000
instead of http://127.0.0.1:8000
(presuming that you run your server on the 8000
port).
Then, clean config and cache:
php artisan config:clear
php artisan cache:clear
Clear cookies (or run in incognito mode)
Since facebook allow localhost
redirect by default, you don't have to whitelist the url in the fb app.
Got the Solution - Update November 2018
public function redirectToGoogle(Request $request)
{
/**
* @var GoogleProvider $googleDriver
*/
$googleDriver = Socialite::driver("google");
return $googleDriver->redirect();
}
public function fromGoogle(Request $request)
{
try {
/*
Solution Starts ----
*/
if (empty($_GET)) {
$t = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
parse_str($t, $output);
foreach ($output as $key => $value) {
$request->query->set($key, $value);
}
}
/*
Solution Ends ----
*/
/**
* @var GoogleProvider $googleDriver
*/
$googleDriver = Socialite::driver("google");
print_r($googleDriver->user());
} catch (\Exception $e) {
print_r($e->getMessage());
}
Also check access right on your storage/framework/sessions
folder.
In my case, since this folder is empty in new Laravel project, it has been left out during initially commit to the GIT repository. Afterwards I created it manually on production server, but obviously with the wrong access rights, hence it was not writable for the session driver (when set to 'file'
).
In my case it was caused by missing parameters in $fillable
array in User
class. When i added all missing parameters, it started to work properly..