laravel socialite not working on live server, only works on local machine
Asked Answered
L

3

5

I have recently moved my application into a live server.
The problem is laravel socialite stopped working, below is the explanation:
I click on google login and after choosing email, on callback this happens:
InvalidStateException on Abstract.php on line 200.
To fixed this issue i used stateless() and now the new error is :
Client error: `POST https://www.googleapis.com/oauth2/v4/token` resulted in a `400 Bad Request` response: { "error": "invalid_request", "error_description": "Missing required parameter: code" }

Here is the list of things i have already tried:
1- In Google website, I updated google api url and callback url to new live domain addresses: http://www.domain-name.com and http://www.domain-name.com/login/google/callback

2- I have modified config/session.php file like this:
'domain' => env('SESSION_DOMAIN', 'www.domain-name.com'),

3- My storage folder is already have permission like this:
drwxrwxr-x 5 root www-data 4096 Jun 24 18:30 storage

4- I changed vendor\laravel\socialite\src\Two\GoogleProvider.php file and removed 'profile' and now it looks like this:

protected $scopes = [
'openid',
'email',
];

5- I used php artisan cache:clear , composer dump-autoload, and cleared browser cache data many times.

6- I tried $userSocial = Socialite::with('google')->user(); instead of $userSocial = Socialite::driver('google')->user(); , and i still get InvalidStateException on Abstract.php on line 200. only when i change to $userSocial = Socialite::driver('google')->stateless()->user(); the error changes to what i said at the beginning of the question.

my config/services.php file is like this:

'google' => [
    'client_id' => 'my-correct-client-id-from-google',
    'client_secret' => 'my-correct-client-secret-from-google',
    'redirect' => 'http://www.domain-name.com/login/google/callback',
],

I have been trying to fix this for 2 days now. any help would be appreciated.

Loftin answered 26/6, 2019 at 19:44 Comment(3)
Do you have an SSL certificate? In production, Google may only provide a code to a HTTPS endpoint to avoid it being intercepted.Cammack
really?! that never came into my head... No i don't have a SSL certificate yet. hope this works.Loftin
it was not the SSL certificate problem.Loftin
L
2

after many hours wasted. finally found the answer:
it was nginx:
so i ran sudo nano /etc/nginx/sites-available/default

and fixed this line:

try_files $uri $uri/ /index.php?query_string; // wrong 

to

try_files $uri $uri/ /index.php?$query_string; // fixed 
Loftin answered 26/6, 2019 at 21:14 Comment(1)
Perfect answer!! Thanks a lot!! Got it resolved after a few dirty hours of search :)Tourism
C
1

Thanks @Pooria Honarmand after hours of spend I found same cause you have mentioned. As I cant change in default config.

Alternatively you can set params like:

$urlArr=parse_url($_SERVER['REQUEST_URI']);
parse_str($urlArr['query'], $output);
$request->merge($output);
Chasteen answered 23/8, 2020 at 15:40 Comment(0)
I
0

I have the same issue i spent more then two days today i updated my composer and remove my vendor folder and install the composer again and the issues has been solved

Iphigeniah answered 24/2, 2021 at 13:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.