Laravel 5 geting InvalidStateException in AbstractProvider.php

前端 未结 12 1880
心在旅途
心在旅途 2020-12-03 22:30

I am trying to use login with facebook in laravel 5 using Socialize.

Here is my route file code.

Route::get(\'fb\', function ($face         


        
相关标签:
12条回答
  • 2020-12-03 22:47

    Go to vendor/guzzlehttp/guzzle/src/Client.php

    $defaults = [
                'allow_redirects' => RedirectMiddleware::$defaultSettings,
                'http_errors'     => true,
                'decode_content'  => true,
                'verify'          => true,
                'cookies'         => false
            ];
    

    change to

    $defaults = [
                'allow_redirects' => RedirectMiddleware::$defaultSettings,
                'http_errors'     => true,
                'decode_content'  => true,
                'verify'          => **false**,
                'cookies'         => false
            ];
    
    0 讨论(0)
  • 2020-12-03 22:50

    http://nhagiaodich.com/dang-nhap

    It work on my website , just call ->stateless() before get user

    Socialite::driver('facebook')->stateless()->user()
    Socialite::driver('google')->stateless()->user()
    
    0 讨论(0)
  • 2020-12-03 22:51

    Make sure, that a query string with url parameters (code and state) are passed to the fpm/mod_php:

    # AbstractProvider.php, line 242
    ...
    protected function hasInvalidState()
    {
        if ($this->isStateless()) {
            return false;
        }
        --> dd($this->request); <--
        $state = $this->request->session()->pull('state');
    
        return ! (strlen($state) > 0 && $this->request->input('state') === $state);
    }
    ...
    

    Find an example of nginx configuration below, that proxies $args variable to the application:

    # example.com.conf
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    

    Make sure, that parameter QUERY_STRING is set in fastcgi_params file:

    # /etc/nginx/includes/fastcgi_params
    ...
    fastcgi_param QUERY_STRING          $args;
    ...
    
    # example.com.conf
    location ~ \.php$ {
        fastcgi_pass fpm:9000;
        fastcgi_index index.php;
        ...
        include /etc/nginx/includes/fastcgi_params;
        ...
    }
    
    0 讨论(0)
  • 2020-12-03 22:52

    I don't know if this will help anyone but changing my session driver from file to cookie solved the InvalidException issue for me.

    0 讨论(0)
  • 2020-12-03 22:53

    For anyone experiencing these problem, you can set the domain value in config/session.php to your domain and clear all cookies in your browser relating to your app url. you can also then run php artisan cache:clear and clear-complied

    0 讨论(0)
  • 2020-12-03 22:55

    I wasn't comfortable with just commenting out code that signalled an error (as in @Dipesh Shihora's answer), so I dug a little further. I discovered that the error is caused (in my case at least) by a problem with sessions.

    My dev server is set up according to the instructions given in this answer. Basically, I am "spoofing" Google by using a callback URL which looks like a publicly-accessible address.

    The InvalidStateException problem was appearing for me because I was visiting my login page at http://localhost/login and redirecting to Google's login page, which then returned me to http://myapp.example.com/callback. The problem is that the session key is stored in a cookie - it was originally a cookie for http://localhost, but when I redirected to a different URL, the cookie (and hence the session key) was inaccessible. Thus, the session state value was non-existent after the update and the exception was thrown.

    The solution? Ensure that all my browsing on the dev machine was done at http://myapp.example.com and not at http://localhost.

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