Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ssionn's avatar

Laravel Socialite GitHub Authentication: Redirect Issue after Successful Authentication

I'm encountering an issue with GitHub authentication in my Laravel application using Laravel Socialite. Despite setting up everything correctly and being able to retrieve user data from GitHub, users aren't redirected to the dashboard after logging in. I've checked my configuration files, but I can't figure out what's wrong.

I made sure my GitHub authentication was set up properly in Laravel Socialite, and I expected users to be redirected to the dashboard after logging in. However, despite successful authentication, users aren't redirected as expected. Instead, they remain on the login page

OAuthController.php:

    public function redirect($github)
    {
        return Socialite::driver($github)
            ->setScopes(['read:user', 'public_repo'])
            ->redirect();
    }

    public function callback($github)
    {
        try {
            $socialUser = Socialite::driver($github)->user();

            $user = User::where([
                'github' => $github,
                'github_id' => $socialUser->id,
            ])->first();

            if (! $user) {
                $user = User::create([
                    'github_id' => $socialUser->id,
                ], [
                    'username' => $socialUser->getNickname(),
                    'name' => $socialUser->getName(),
                    'email' => $socialUser->getEmail(),
                    'github' => $socialUser->github,
                    'github_token' => $socialUser->token,
                ]);
            }

            Auth::attempt($user);

            return redirect()->intended(route('dashboard'));
        } catch (\Exception $e) {


            return redirect()->route('login')->with('error', 'Something went wrong with ' . $github . ' login');
        }
    }

This resulted in Socialite actually fetching the data, but not doing anything with it. The config for this is correct, otherwise it would return a redirect_uri error, but this was not the case. Routing is fine too, socialite wouldn't have been able to fetch otherwise. Which leads me to believe it has something to do with the Query Builder.

But despite my efforts, I'm unsure of what steps to take next. Any advice or suggestions on how to proceed would be greatly appreciated.

0 likes
6 replies
shariff's avatar

Are you getting any error? I feel Here is the problem

 $user = User::where([
                'github' => $socialUser->github,  //this line
                'github_id' => $socialUser->id,
            ])->first();

Ssionn's avatar

@shariff $github is the provider i'm passing through as a variable from the route, $socialUser->github isn't actually a field inbetween the data i'm receiving.

Ssionn's avatar

@shariff I should also mention that it is creating a session in the database.

shariff's avatar
shariff
Best Answer
Level 50

@Ssionn Try adding dd in the catch block and check are you getting any errors dd($e)

1 like
Ssionn's avatar

@shariff Ah it's a 401 response. It shows a 302 through the network tab though. Strange. Anyways, my tokens should be fine, but I think it has something to do with the GitHub OAuth application.

shariff's avatar

@Ssionn Ensure that all tokens and permissions are assigned correctly, as any missing scopes or permission issues with the account may be the cause.

Please or to participate in this conversation.