Laravel Socialite: InvalidStateException

后端 未结 25 984
感动是毒
感动是毒 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:13

    I had the same problem and I just cleared the cache to solve this problem. I just ran this command and started the process again.

    php artisan cache:clear
    

    I hope this answer may help someone.

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

    If you still need help you can use my code, it works for me. You just need to create two routes and update the users table. Don't forget to make password nullable, since you won't get one from the facebook users The code in my controller:

    public function redirectToProvider()
    {
        return Socialize::with('facebook')->redirect();
    }
    
    public function handleProviderCallback(User $user)
    {
        $money = Socialize::with('facebook')->user();
    
        if(User::where('email', '=', $money->email)->first()){
        $checkUser = User::where('email', '=', $money->email)->first();
        Auth::login($checkUser);
        return redirect('home');
         } 
    
        $user->facebook_id = $money->getId();
        $user->name = $money->getName();
        $user->email = $money->getEmail();
        $user->avatar = $money->getAvatar();
        $user->save();
    
        Auth::login($user);
        return redirect('home');
    
    }
    
    0 讨论(0)
  • 2020-11-27 14:14

    Finally i solved by :

    Redirect URL in .env file should be the same URL in google developer account

    double check on your redirects urls .

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

    I stacked on this two day and changing 'driver' => env('SESSION_DRIVER', 'file')to 'driver' => env('SESSION_DRIVER', 'cookie') worked for me

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

    I want to share you my solution . I go to my AbstractProvider.php file and in the line of problem

    public function user()
    {
        if ($this->hasInvalidState()) {
            throw new InvalidStateException;
        }
    
        // ...
    }
    

    I stop the throw new InvalidStateException and call the redirect function like that:

    public function user()
    {
        if ($this->hasInvalidState()) {
            $this->redirect();
            // throw new InvalidStateException;
        }
    
        // ...
    }
    
    0 讨论(0)
  • 2020-11-27 14:17

    There are 2 major "gotchas" that none of the existing answers address.

    1. Sometimes InvalidStateException is a red herring and the true root cause is some other bug. It took me ~12 hours one time to realize that I hadn't added a new field to the $fillable array in the model.
    2. Unless you disable session state verification (as other answers here seem to want you to do but not everyone will want to do), $provider->user() can only be called once per request because the inside of that function calls hasInvalidState(), which then removes the 'state' entry from the session. It took me hours to realize that I happened to be calling $provider->user() multiple times, when I should have called it just once and saved the result to a variable.
    0 讨论(0)
提交回复
热议问题