laravel / socialite

Laravel wrapper around OAuth 1 & OAuth 2 libraries.
https://laravel.com/docs/socialite
MIT License
5.58k stars 940 forks source link

ID, Token, and RefreshToken not being populated #699

Closed nexxai closed 7 months ago

nexxai commented 7 months ago

Socialite Version

5.13

Laravel Version

11.4

PHP Version

8.3.6

Database Driver & Version

No response

Description

So I've got the auth redirect/callback flow working but even when using the exact code from the docs, the google_id, google_token, and google_refresh_token values are not populating in the database. This means that if we want to add a second oAuth provider, there would be no way to distinguish an account from Google and future provider 2 if they share an email address.

My code:

    Route::get('/auth/google/redirect', function () {
        return Socialite::driver('google')->redirect();
    })->name('auth.google.redirect');

    Route::get('/auth/google/callback', function () {
        $googleUser = Socialite::driver('google')->user();

        $user = User::updateOrCreate([
            'google_id' => $googleUser->id,
        ], [
            'name' => $googleUser->name,
            'email' => $googleUser->email,
            'google_token' => $googleUser->token,
            'google_refresh_token' => $googleUser->refreshToken,
        ]);

        Auth::login($user);

        return redirect(route('post-a-fit'));
    });

To be clear, the login and auth works and the person is considered logged into the app with an entry in the users table, but those fields not being populated is worrying.

Steps To Reproduce

  1. Follow the "Login with Google" flow
  2. Successfully authenticate at accounts.google.com
  3. Be redirected to the site successfully
  4. Look at the database
nexxai commented 7 months ago

I realized I'm an idiot and forgot to add the fields to the $fillable array. Apologies.