Asked  7 Months ago    Answers:  5   Viewed   60 times

I'm using Laravel Socialite to add a Facebook connect button on a website. Sometimes, I've got this error on callback:

exception 'LaravelSocialiteTwoInvalidStateException' 
in /example/vendor/laravel/socialite/src/Two/AbstractProvider.php:161

I don't know what it mean and did not found anything yet about this error. The real problem is it seems to be a random exception (don't understood why it happens). So what this error means and how to avoid it?

It seems it's not the same problem as Laravel 5 geting InvalidStateException in AbstractProvider.php, cause in my case it's random.

 Answers

69

I ran into this issue last night and solve it with the following solution.

More information on my issue, I've got

InvalidStateException in AbstractProvider.php line 182

in the function handleProviderCallback() when it re-direct 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 - Thank you very much.

The Solution

  1. Go to your www root, check the laravel file config/session.php
  2. Check session Session Cookie Domain The default configuration is 'domain' => null, I made a change to 'domain' => 'mysite.com'.
  3. 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.

Wednesday, March 31, 2021
 
phpmeh
answered 7 Months ago
62

After a long search, the solution is to remove the profile from scopes in vendorlaravelsocialitesrcTwoGoogleProvider.php

protected $scopes = [
    'openid',
    'email',
];
Friday, May 28, 2021
 
Sufi
answered 5 Months ago
23

I solved it

I searched a lot for a solution to this problem and found that many others also experienced it. If you only need one element on the other end of your relation, it's very straight forward.

The addition of the "multi column unique restriction" is what made this complicated. The only solution I found was "Forget the MySQL restriction and just surround the factory creation with a try-catch for PDO-exceptions". This felt like a bad solution since other PDOExceptions would also get caught, and it just didn't feel "right".

Solution

To make this work I divided the seeders to ImageTableSeeder and ImageTextTableSeeder, and they are both very straight forward. Their run commands both look like this:

public function run()
{
    factory(AppModelsImageText::class, 100)->create();
}

The magic happens inside the ImageTextFactory:

$factory->define(AppModelsImageText::class, function (FakerGenerator $faker) {

    // Pick an image to attach to
    $image = AppModelsImage::inRandomOrder()->first();
    $image instanceof AppModelsImage ? $imageId = $image->id : $imageId = null;

    // Generate unique imageId-languageCode combination
    $imageIdAndLanguageCode = $faker->unique()->regexify("/^$imageId-[a-z]{2}");
    $languageCode = explode('-', $imageIdAndLanguageCode)[1];

    return [
        'image_id' => $imageId,
        'language' => $languageCode,
        'title' => $faker->word,
        'text' => $faker->text,
    ];
});

This is it:

$imageIdAndLanguageCode = $faker->unique()->regexify("/^$imageId-[a-z]{2}");

We use the imageId in a regexify-expression and add whatever is also included in our unique combination, separated in this case with a '-' character. This will generate results like "841-en", "58-bz", "96-xx" etc. where the imageId is always a real image in our database, or null.

Since we stick the unique tag to the language code together with the imageId, we know that the combination of the image_id and the languageCode will be unique. This is exactly what we need!

Now we can simply extract the created language code, or whatever other unique field we wanted to generate, with:

$languageCode = explode('-', $imageIdAndLanguageCode)[1];

This approach has the following advantages:

  • No need to catch exceptions
  • Factories and Seeders can be separated for readability
  • Code is compact

The disadvantage here is that you can only generate key combinations where one of the keys can be expressed as regex. As long as that's possible, this seems like a good approach to solving this problem.

Saturday, May 29, 2021
 
PLPeeters
answered 5 Months ago
51

You are using join instead of left join

Try this

DB::table('A')
->leftjoin('B', 'A.program_id', '=', 'B.program_id')
->select('A.program_id')
->whereNull('B.program_id')
->where('A.student_id', '=', 5)
->get()->toArray();

It will produce a query like enter image description here

Saturday, August 7, 2021
 
Parfait
answered 3 Months ago
67

Try this, I'm using Laravel 5.5:

return Socialite::driver('google')->stateless()->user();

https://laravel.com/docs/5.5/socialite#stateless-authentication

Sunday, October 3, 2021
 
EzzDev
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :