1

I'm trying to add login with social media

I'm using Laravel 7 and socialite for login.

It was working at first but after the first time I'm getting following error:

Laravel\Socialite\Two\InvalidStateException
vendor/laravel/socialite/src/Two/AbstractProvider.php:210

I have read this question Laravel Socialite: InvalidStateException but I couldn't solve the problem.

I would appreciate any help

  • try to use ->stateless() method in redirect method `return Socialite::with($driver)->with()->stateless()->redirect()`, in handle() `$externalUser = Socialite::driver($driver)->stateless()->user();` – Bohdan Petrenko Mar 26 '20 at 16:17
  • @BohdanPetrenko, I got this error: Too few arguments to function Laravel\Socialite\Two\AbstractProvider::with(), 0 passed in /home/mohammad/projects/krafixel/app/Http/Controllers/LoginSocial.php on line 12 and exactly 1 expected – mohammad pakivand Mar 26 '20 at 16:26
  • try without `->with()` it was an example). In `->with()` u use some provider settings, like for google `->with(["prompt" => "select_account", 'response_type' => 'code',)`... – Bohdan Petrenko Mar 26 '20 at 16:37
  • ```Socialite::with($driver)->stateless()->redirect()``` – Bohdan Petrenko Mar 26 '20 at 16:41
  • @BohdanPetrenko it didn't work – mohammad pakivand Mar 27 '20 at 08:21

2 Answers2

0

In my case the same error was caused by adding a reserved word parameter "state" to the ->with() method. I did:

return Socialite::driver('twitch')
    ->with(['state' => 'randomstate'])
    ->redirect();

Removing that helped.

Serge
  • 1,531
  • 2
  • 21
  • 44
0

I had the same issue, but only when running the application (Laravel 8) on the production host.

The problem was the enforcement of a restrictive Resource Isolation Policy from the provider, which discarded all input parameters for requests having a sec-fetch-site:cross-site header (which is the case of an OAuth2 redirect).

To verify if this is your case, it is enough to check the contents of the Request coming to your endpoint function: if the following code prints an empty array, you probably have to check your web server configurations.

public function socialCallback(Request $request) {
  // Those two lines are just for debug, to be removed
  print_r($request->all());
  exit();

  Socialite::driver('google')->user();
}

Here, more informations about sec-fetch-site.

madbob
  • 456
  • 1
  • 6
  • 13