-
Notifications
You must be signed in to change notification settings - Fork 940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Google Refresh Token not always given back, intermittent #715
Comments
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
Google returns a refresh token only when the consent screen is displayed and a user has granted the requested permissions to your app. All subsequent requests to the Google OAuth API do not provide a refresh token. Socialite::driver('google')->with(array_merge([
'access_type' => 'offline',
'prompt' => 'consent',
], [
'state' => 'integration_id='.$request->input('integration_id', '')
]))->redirect(); However, the official Google documentation includes a warning for such cases:
Therefore, request refresh tokens only when you really need them, and store them safely and permanently. |
@ev-gor Thanks for that! Could you link me to the docs where you'd got that from please? Does this mean then that I should do a check and if the returned user contains null for refresh token to just use the initial one stored in the database? Additionally, the "expires in" fetched back is for the access token, not refresh token unless I'm mistaken? Worth noting on this, I have a cron than runs every 15 minutes on my server that grabs all tokens which are due to expire based on the expires in, and it calls the /**
* Refresh the token
*/
private function refreshToken(GoogleToken $token): void
{
if (! $token->refresh_token) return;
$newToken = Socialite::driver('google')->refreshToken($token?->refresh_token);
if ($newToken?->token) {
$token->update([
'access_token' => $newToken?->token,
'refresh_token' => $newToken?->refreshToken ?? ($token->refresh_token ?: null),
'expires_in' => $newToken?->expiresIn ?? 0,
'last_refresh_at' => now()
]);
}
}``` |
Google has very detailed documentation about its OAuth flow. For Laravel developers this article is especially useful. |
@ev-gor So do you think, in the case of my project, that it's safe then to call $newToken?->refreshToken ?? ($token->refresh_token ?: null) This should retain the existing token held in |
@sts-ryan-holton Sure, you can use the |
Socialite Version
5.16.0
Laravel Version
11.23.5
PHP Version
8.3.7
Database Driver & Version
MySQL 8
Description
When Google returns the user to the
callback
URL, therefreshToken
field isn't always set and is sometimesnull
. This means that although I can store thetoken
, it will expire after an hour and then no longer have a way to refresh it. I'm not sure why it's sometimesnull
, maybe there needs to be a way to revoke first? Or whether there's something else going on here.Steps To Reproduce
User
from the callback:The text was updated successfully, but these errors were encountered: