You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Supabase.initialize don't refresh token after 1h on app open again #906
Describe the bug
From 1 week and a couple days ago, we have a problem in our application, basically, before this time, users use login with OTP normally, and have a refresh token automatically, every good.
But now, after this days, when user login with OTP, this generate a valid token, but, if they close and access app again after 1 hour, this is logged out, because the token is no more valid.
The problem is, i use Supase from 1 year, and never have this problem, i use Supabase.initialize all time on start app, and this check if the old token is valid, if not, refresh that. But now, don't work anymore.
Debbuging, i see the client.auth.session <- its comming null on app after this 1hour time.
To Reproduce
Only starts Supabase.initialiize, this generate a token in console -> put app closed in background, and re-open after a medium time, this retorn the same token (don't refresh).
Expected behavior
The token needs to be refresh when i open app again, when the old token its not valid, and goTrue Session not be null.
Screenshots
Version (please complete the following information):
On Linux/macOS
I'm using latest version of Flutter, and latest version of Supabase supabase_flutter 2.5.1.
On Windows
Additional context
I think explained all infos.
Reacted by Tom Raganowicz, nietsmmar, Arbaz Irshad, Shreeman Arjun Sahu and 0xPixelfrost
changed the title [-]Supabase.initialiize don't refresh token after 1h on app open again[/-][+]Supabase.initialize don't refresh token after 1h on app open again[/+]on Apr 26, 2024
@gabrielviannadev I cannot reproduce this on my end, but are you able to reproduce it? Do you have reliable steps to reproduce every time, or do you see this happening only once in a while?
i am also facing the issue but i is not happening consistently but many times when app is opened after some time is gives below error and user gets sign out.
I experience the same problem on my production app - users are randomly logged out from the app. In Supabase logs, I see exactly the same log as @com8member2 included above. Please bump priority on it!
In our case when app is closed for more than an hour, token expires which is expected. Problem happens when app is started again, we then issue the API request using the most recent JWT (which has expired) which results in API call failing with 403 Forbidden.
The: _callRefreshToken logic auto refreshes the token successfully, but new token is received shortly (depending on network conditions etc.) after the initial request was issued.
Perhaps: _refreshTokenCompleter could be exposed, so we could either await on it if it's Future or even run: _callRefreshToken ourselves to ensure that latest JWT is fetched before any API call is made.
@tomekit What's your use case to manually use the latest jwt? If you are calling supabase endpoints via the sdk, the sdk should try to get a new jwt before making the actual request.
My point is, that if Supabase Flutter allows user to access current session:
Session?get currentSession => _currentSession;
it already leaks enough internals (and that's good, because it allows JWT external use) that it should probably allow user to access Future to await to make sure that currentSession is up to date, but that's my opinion only.
Calling refreshSeession() if the session is expired (session.isExpired) should fix it. It returns the future to the already ongoing request or starts a new refresh.
Thanks for you reply. I've just double checked our codebase and that's actually what we've been doing already... and in fact it should behave as you describe.
Sorry for the confusion caused, we're still getting some intermittent issues with not up to date token, but it seems that this issue likely lies somewhere on our end, as Flutter's goauth library resolved most of these over the past months.
Your access token is being refreshed, but you are trying to connect to Realtime before you receive the new accessToken, which is causing this error is what I think is happening here. This should certainly be handled by the client, so we will confirm on our end if this is really happening, but make necessary changes.
For now, you can just make sure to only connect to realtime after you receive the tokenRefreshed auth event.
I ran a minimal flutter web app for over an hour and had no issues. Could it be the case you are affected by the following issue: #1164 So basically you are using the new shared preference apis, which are incompatible with us still using the old and soon deprecated apis?
Activity
[-]Supabase.initialiize don't refresh token after 1h on app open again[/-][+]Supabase.initialize don't refresh token after 1h on app open again[/+]dshukertjr commentedon Apr 26, 2024
@gabrielviannadev I cannot reproduce this on my end, but are you able to reproduce it? Do you have reliable steps to reproduce every time, or do you see this happening only once in a while?
com8member2 commentedon May 1, 2024
i am also facing the issue but i is not happening consistently but many times when app is opened after some time is gives below error and user gets sign out.
marektomczyk commentedon May 24, 2024
I experience the same problem on my production app - users are randomly logged out from the app. In Supabase logs, I see exactly the same log as @com8member2 included above. Please bump priority on it!
dshukertjr commentedon May 28, 2024
@com8member2 @marektomczyk
So this random logout happens not when the user launches the app but when they have the app opened for a long time?
tomekit commentedon Jun 7, 2024
In our case when app is closed for more than an hour, token expires which is expected. Problem happens when app is started again, we then issue the API request using the most recent JWT (which has expired) which results in API call failing with 403 Forbidden.
The:
_callRefreshTokenlogic auto refreshes the token successfully, but new token is received shortly (depending on network conditions etc.) after the initial request was issued.Perhaps:
_refreshTokenCompletercould be exposed, so we could eitherawaiton it if it's Future or even run:_callRefreshTokenourselves to ensure that latest JWT is fetched before any API call is made.Vinzent03 commentedon Jun 15, 2024
@tomekit What's your use case to manually use the latest jwt? If you are calling supabase endpoints via the sdk, the sdk should try to get a new jwt before making the actual request.
zhangheng2022 commentedon Jul 1, 2024
Maybe we need an example
tomekit commentedon Jul 1, 2024
Hi @Vinzent03,
Sorry for late reply. In our case we're using Supabase mostly for auth and then pass JWT to external APIs outside of Supabase ecosystem.
I think it's fair to assume that there are many systems (legacy or not) which would like to integrate authentication mechanism and not necessarily would like to be rewritten using Supabase mindset.
Given that JWT integration is extremely easy I think it's rather common use case to "connect" other systems to unified auth system which Supabase's GoTrue is (https://github.com/supabase/gotrue).
The current downside is that Supbase doesn't support public keys, so currently sharing secret to external API shares private key, which isn't inherently secure: https://github.com/orgs/supabase/discussions/12759#discussioncomment-9584184)
It would be also good to allow optional 2FA JWT verification, we've added that ourselves: supabase/auth@8878f32 but that's probably bit offtopic.
My point is, that if Supabase Flutter allows user to access current session:
it already leaks enough internals (and that's good, because it allows JWT external use) that it should probably allow user to access Future to await to make sure that
currentSessionis up to date, but that's my opinion only.Vinzent03 commentedon Jul 2, 2024
Calling
refreshSeession()if the session is expired (session.isExpired) should fix it. It returns the future to the already ongoing request or starts a new refresh.tomekit commentedon Jul 2, 2024
Thanks for you reply. I've just double checked our codebase and that's actually what we've been doing already... and in fact it should behave as you describe.
Sorry for the confusion caused, we're still getting some intermittent issues with not up to date token, but it seems that this issue likely lies somewhere on our end, as Flutter's
goauthlibrary resolved most of these over the past months.dshukertjr commentedon Sep 4, 2024
@vikrvm Do you have a way to reproduce this issue consistently? That would help us figure out what the root cause is.
dshukertjr commentedon Sep 4, 2024
@vikrvm Is it a Swift or Flutter application running on iOS?
7 remaining items
dshukertjr commentedon Dec 15, 2024
@zcmgyu
Your access token is being refreshed, but you are trying to connect to Realtime before you receive the new accessToken, which is causing this error is what I think is happening here. This should certainly be handled by the client, so we will confirm on our end if this is really happening, but make necessary changes.
For now, you can just make sure to only connect to realtime after you receive the
tokenRefreshedauth event.zcmgyu commentedon Dec 16, 2024
@dshukertjr Thank you. Just to confirm, I’ve already addressed this case across all real-time channels, and I hope it resolves the issue.
dshukertjr commentedon Dec 16, 2024
@zcmgyu Not sure if that will fix the issue, but did it work?
Once this version is released, it should fix the issue for you.
zcmgyu commentedon Dec 17, 2024
@dshukertjr I'm not sure it worked. Until now, that issue did not occur. Waiting for it :D
tomekit commentedon Dec 29, 2024
In our case token wasn't automatically refreshing in the background after upgrading to 2.8.2 release.
We've downgraded to 2.8.0 and things are back to normal.
This is applicable to web version of our app: https://web.s3drive.app/
Desktop and mobile seem to be unaffected and refreshing takes places normally on 2.8.2.
dimeloper commentedon May 26, 2025
Are there any updates here? We're experiencing the same issue. I suppose we should also downgrade to version 2.8.0?
Vinzent03 commentedon May 28, 2025
I ran a minimal flutter web app for over an hour and had no issues. Could it be the case you are affected by the following issue: #1164 So basically you are using the new shared preference apis, which are incompatible with us still using the old and soon deprecated apis?
fix(auth): proactively refresh expired tokens on app resume
github-actions commentedon Mar 1, 2026
This issue has been inactive for 180 days.
It will be automatically closed in 30 days if there is no further activity.
If you believe this issue is still relevant, please leave a comment to keep it open.
Thank you for your contributions!
0xPixelfrost commentedon Mar 22, 2026
Any updates on this? My user still get logged out over night.
If I had known beforehand that this would become such a long and complex issue, I wouldn't have chosen Supabase. This is an essential feature.
fix(auth): avoid reusing a stale refresh token in recoverSession (#1391)
fix(gotrue): prevent stale token refresh from overwriting concurrent …