Laravel Socialite: InvalidStateException

后端 未结 25 987
感动是毒
感动是毒 2020-11-27 13:56

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\         


        
相关标签:
25条回答
  • 2020-11-27 14:18

    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.

    0 讨论(0)
  • 2020-11-27 14:18

    this solved it for me
    $request->session()->put('state',Str::random(40)); $user = Socialite::driver('github')->stateless()->user();

    0 讨论(0)
  • 2020-11-27 14:18

    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.

    0 讨论(0)
  • 2020-11-27 14:21

    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());
        }
    
    0 讨论(0)
  • 2020-11-27 14:23

    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').

    0 讨论(0)
  • 2020-11-27 14:23

    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..

    0 讨论(0)
提交回复
热议问题