Repository navigation
Description
Describe the bug
We had this week quite a lot of users where the JWT expired.
There users where not logged out instead they had to close and reopen the app to trigger the logout.
In our logs we have seen a lot of these errors:
data:text/text;charset=utf-8,
PostgrestException(message: JWT expired, code: PGRST301, details: Unauthorized, hint: null)
When the exception was thrown, this was the stack:
0 PostgrestBuilder._parseResponse (package:postgrest/src/postgrest_builder.dart:323:0)
1 PostgrestBuilder._execute (package:postgrest/src/postgrest_builder.dart:198:0)
2 PostgrestBuilder.then (package:postgrest/src/postgrest_builder.dart:400:0)
To Reproduce
Steps to reproduce the behavior:
- Setup a listener to listen for onAuthStateChange (we use a bloc cubit for that )
- Login
- Let the JWT expire
- Try to make a request
Expected behavior
User is getting logged out either when the JWT expires or when the try to make a request.
Screenshots
Version (please complete the following information):
On Linux/macOS
Please run dart pub deps | grep -E "supabase|gotrue|postgrest|storage_client|realtime_client|functions_client" in your project directory and paste the output here.
│ └── supabase_flutter...
│ └── supabase_flutter...
│ └── supabase_flutter...
│ ├── supabase_flutter...
│ ├── supabase_flutter...
│ └── supabase_flutter...
│ ├── supabase_flutter...
│ └── supabase_flutter...
│ ├── supabase_flutter...
│ └── supabase_flutter...
│ └── supabase_flutter...
│ └── supabase_flutter...
├── supabase_flutter 1.7.0
│ ├── supabase 1.6.3
│ │ ├── functions_client 1.1.1
│ │ ├── gotrue 1.6.0
│ │ ├── postgrest 1.2.3
│ │ ├── realtime_client 1.0.3
│ │ ├── storage_client 1.3.0
On Windows
Please run dart pub deps | findstr "supabase gotrue postgrest storage_client realtime_client functions_client" in your project directory and paste the output here.
Additional context
Add any other context about the problem here.
Activity
Vinzent03 commentedon Apr 25, 2023
gotrue 1.6.0got just a few minutes ago released. Do you really still experience this issue? What version ofgotruewhere the production app using? What jwt expiry time have you set?KirioXX commentedon Apr 26, 2023
Thanks for the quick response @Vinzent03.
That is a very good point the current production version is still on
gotrue 1.4.2:Could that be the issue?
Vinzent03 commentedon Apr 26, 2023
There was a fix in 1.5.6 about jwt expiry margins. This may be the cause.
KirioXX commentedon Apr 26, 2023
Thank you very much Vinzent03.
We actually have a new release going out to the first users today, which uses 1.5.6.
I will keep you posted if the new version resolves it.
Vinzent03 commentedon Apr 26, 2023
It might be important to update to 1.5.7 though, depending on how important the session emitted by the inAuthStateChange stream is.
KirioXX commentedon Apr 26, 2023
Sorry I meant we are on 1.5.7 😅
KirioXX commentedon May 5, 2023
@Vinzent03 we rolled out our new app version to some test device. So far everything looks good.
But we got this error yesterday and it seems to be stuck in a loop:
Can that be related to the JWT issue?
Vinzent03 commentedon May 5, 2023
You should get logged out when this error comes up. You get this error for example when you sign out on another device.
KirioXX commentedon May 12, 2023
Hi @Vinzent03, we had yesterday again a user that didn't get log out even though the request returned 401 codes.
Is there anything that we could miss when we listen to onAuthStateChange?
Vinzent03 commentedon May 12, 2023
We currently only sign out on the specific error message
Invalid Refresh Token: Refresh Token Not Found. I don't know what the reason for a 401 response is. We emit the error viaaddErrorto the onAuthDtateChange stream, where you could react to that.KirioXX commentedon May 12, 2023
Thanks for the quick response @Vinzent03.
If it helps, this is full response:
The logs also say that there was 1 slow request before the failing once, could it be that the JWT was invalidated because the refresh request was to slow? Sadly I miss a couple network request logs.
Vinzent03 commentedon May 12, 2023
That seems to be the response of a call to postgrest which failed, because the call to refresh the jwt didn't work.
Can you see how slow exactly the previous request was and where it went? Maybe the call to the token endpoint to refresh the jwt hasn't finished yet?
KirioXX commentedon May 12, 2023
I can see the 100 failed requests and they all tried to fetch the same resource:
{ "status": 401, "response_time": 198.431, "method": "GET", "response_headers": { "access-control-allow-origin": "*", "content-type": "application/json; charset=utf-8", "x-kong-upstream-latency": "0", "alt-svc": "h3=\":443\"; ma=86400, h3-29=\":443\"; ma=86400", "via": "kong/2.8.1", "server": "cloudflare", "sb-gateway-version": "1", "transfer-encoding": "chunked", "cf-cache-status": "DYNAMIC", "date": "Thu, 11 May 2023 21:49:44 GMT", "x-kong-proxy-latency": "1", "strict-transport-security": "max-age=2592000; includeSubDomains", "connection": "keep-alive", "cf-ray": "7c5da231ee9d0fd3-LAX", "vary": "Accept-Encoding", "www-authenticate": "Bearer error=\"invalid_token\", error_description=\"JWT expired\"" }, "date": 1683841784800, "request": "", "headers": { "Accept-Profile": "public", "apikey": "XXXX", "X-Client-Info": "supabase-flutter/1.7.0", "Authorization": "Bearer XXXX" }, "response": { "code": "PGRST301", "message": "JWT expired", "details": null, "hint": null }, "url": "https://XXXX/rest/v1/job_cards?select=%2A&job_card_id=eq.ae44c164-a38b-413a-9e61-ab89512a4bb7&limit=1" }KirioXX commentedon May 15, 2023
Hi @Vinzent03,
I just wanted to check if there are any updates on this issue?
If that is nothing new could you advice use how we could get around this issue?
Thank you
37 remaining items
Vinzent03 commentedon Jul 30, 2023
Yeah your code should generally work. Two things though to consider
isExpiredgetter on the session, because it adds a 5 second buffer.KirioXX commentedon Aug 24, 2023
Hi @dshukertjr, sorry for the delayed response.
We where not able to test it, but we just started rolling out to a new customer so I should be able to give you feedback in a couple days on this issue.
What though noticed is that we have with the new version some strange behaviour with the streams, I created a new issue for that: #604
bretmh commentedon Oct 27, 2023
Error:
PostgrestException(message: JWT expired, code: PGRST301, details: Unauthorized, hint: null).I'm curious about the progress of this issue. Starting my application I am already logged in, the event AuthChangeEvent.signedIn is fired (session is recovered).
I've checked the calls the state changes and also the session recovery in
supabase_auth.dartit all looks fine. It's failing on an RPC call, I've also checked thepostgrest_builder.dartand the configuration of the AuthHttpClient. There is nothing wrong, however, the backend doesn't like it. How I've determined it's okay is the expiresAt and currentTime values being used results in the token are still valid.dshukertjr commentedon Jan 22, 2024
I'm going to close this issue as we have implemented many updates to mitigate the original issue of the access token not refreshing properly, but if anyone is still experiencing the same issue, please let us know in the comments and we will re-open this one.
tomekit commentedon Jan 22, 2024
In our case we get expired token in our login handler every time when user starts the app when stored access token (1h expiration time already expired)
Likely because:
tokenRefreshedevent is sent before:_saveSessionfinishes, so any code that relies on:tokenRefreshedevent and:Supabase.instance.clientwill generate 403 until the_saveSessionfinalizes.gotrue-2.3.0/lib/src/gotrue_client.dart
dshukertjr commentedon Jan 22, 2024
@tomekit
In our case we get expired token in our login handler every time when user starts the app when stored access token (1h expiration time already expired)
The token being expired on app launch shouldn't be an issue as the token should be automatically refreshed before making an API request.
Do you have any code that you use that relies on
tokenRefreshedevent?What exactly are you experiencing? Do you get
PostgrestExceptionwith error codePGRST301thrown when making API request?fix(auth): proactively refresh expired tokens on app resume
github-actions commentedon Mar 22, 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!
github-actions commentedon Apr 26, 2026
This issue was automatically closed due to inactivity.
If you believe this is still relevant, feel free to reopen it or create a new issue with updated information.
fix(auth): avoid reusing a stale refresh token in recoverSession (#1391)