Jump to content

Getting this error Laravel\Socialite\Two\InvalidStateException


vusal5555

Recommended Posts

Hi to everyone. I have been trying to implement social auth into application. I defined github and google auth credentials in my .env file. I access them in my services.php

 

 'github' => [
        'client_id' => env('GITHUB_CLIENT_ID'),
        'client_secret' => env('GITHUB_CLIENT_SECRET'),
        'redirect' => '/auth/github/callback',
    ],

    'google' => [
        'client_id' => env('GOOGLE_CLIENT_ID'),
        'client_secret' => env('GOOGLE_CLIENT_SECRET'),
        'redirect' => '/auth/google/callback',
    ],

Here is my controller as well

<?php

namespace App\Http\Controllers;

use Laravel\Socialite\Facades\Socialite;

class ProviderController extends Controller
{
    public function redirect($provider)
    {
        return Socialite::driver($provider)->redirect();
    }

    public function callback($provider)
    {
        $user = Socialite::driver($provider)->user();

        dd($user);
    }
}

When I try to hit these two endpoints I receive the above error.

Route::get('/auth/{provider}/redirect', [\App\Http\Controllers\ProviderController::class, 'redirect'])->name('github.redirect');
Route::get('/auth/{provider}/callback', [\App\Http\Controllers\ProviderController::class, 'callback']);

 

I checked my enviroment variables and ran the tinker command and have access to them. I also cleared the cache. when I logged the error I got this message 

[2024-10-18 05:06:24] local.ERROR: Authentication error: {"provider":"github","message":"Client error: `GET https://api.github.com/user` resulted in a `401 Unauthorized` response:
{\"message\":\"Bad credentials\",\"documentation_url\":\"https://docs.github.com/rest\",\"status\":\"401\"}
"} 

 

Link to comment
Share on other sites

23 hours ago, requinix said:

It pretty clearly says "bad credentials" in the error message...

What does it mean by bad credentials though? if it is credentials like client id and client secret, I have already defined them in my env file and checked by logging them in the tinker console.

Edited by vusal5555
Link to comment
Share on other sites

Do a dd() with the credentials to make sure you've actually got what you think. You may need to clear your caches (php artisan optimize:clear). And of course, verify that the credentials you're using are correct and valid.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.