Repository navigation
Description
Describe the bug
I need to maintain a Custom LocalStorage and followed the description here: https://pub.dev/documentation/supabase_flutter/latest/#a-idcustom-localstorageacustom-localstorage
I am encountering a recurring issue in my iOS Flutter application that integrates Supabase with custom LocalStorage for authentication. Specifically, when attempting to automatically recover a user session after the application has been in the background, the refresh token fails with an "Invalid Refresh Token: Already Used" error. This error is consistently triggering a sign-out event, effectively logging the user out and hindering session persistence across app launches or background returns.
To Reproduce
Steps to reproduce the behavior:
- Log in to the application to initiate a session with Supabase.
- Send the application to the background for an extended period or until the access token is likely expired.
- Resume the application, triggering the automatic session recovery process.
- Observe that instead of refreshing the session, an error occurs, and the user is signed out.
Expected behavior
The application should silently refresh the session using the stored refresh token when the access token has expired, without any intervention from the user, maintaining the user's logged-in state across app uses. This works for the default localstorage of Supabase.
Actual beheavior
When attempting to refresh the session, the application signs out and throws an "Invalid Refresh Token: Already Used" error, resulting in the user being signed out. The relevant log outputs are as follows:
flutter: **** onAuthStateChange: AuthChangeEvent.signedOut
flutter: Auth state change detected
flutter: AuthException(message: Invalid Refresh Token: Already Used, statusCode: 400)
flutter: #0 GotrueFetch.request (package:gotrue/src/fetch.dart:99:7)
flutter: <asynchronous suspension>
flutter: #1 GoTrueClient._callRefreshToken (package:gotrue/src/gotrue_client.dart:1087:24)
flutter: <asynchronous suspension>
flutter: #2 GoTrueClient.recoverSession (package:gotrue/src/gotrue_client.dart:928:16)
flutter: <asynchronous suspension>
flutter: #3 SupabaseAuth._recoverSupabaseSession (package:supabase_flutter/src/supabase_auth.dart:127:7)
flutter: <asynchronous suspension>
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: AuthException(message: Invalid Refresh Token: Already Used, statusCode: 400)
#0 GotrueFetch.request (package:gotrue/src/fetch.dart:99:7)
<asynchronous suspension>
#1 GoTrueClient._callRefreshToken (package:gotrue/src/gotrue_client.dart:1087:24)
<asynchronous suspension>
#2 GoTrueClient.recoverSession (package:gotrue/src/gotrue_client.dart:928:16)
<asynchronous suspension>
#3 SupabaseAuth._recoverSupabaseSession (package:supabase_flutter/src/supabase_auth.dart:127:7)
<asynchronous suspension>
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: AuthException(message: Invalid Refresh Token: Already Used, statusCode: 400)
#0 GotrueFetch.request (package:gotrue/src/fetch.dart:99:7)
<asynchronous suspension>
#1 GoTrueClient._callRefreshToken (package:gotrue/src/gotrue_client.dart:1087:24)
<asynchronous suspension>
#2 GoTrueClient.recoverSession (package:gotrue/src/gotrue_client.dart:928:16)
<asynchronous suspension>
#3 SupabaseAuth._recoverSupabaseSession (package:supabase_flutter/src/supabase_auth.dart:127:7)
<asynchronous suspension>
Version (please complete the following information):
├── supabase_flutter 2.3.4
│ ├── supabase 2.0.8
│ │ ├── functions_client 2.0.0
│ │ ├── gotrue 2.5.1
│ │ ├── postgrest 2.1.1
│ │ ├── realtime_client 2.0.1
│ │ ├── storage_client 2.0.1
Activity
micheltucker commentedon Apr 22, 2024
cc: @dshukertjr
I saw you working on the custom LocalStorage implementation and maybe you can help :)
micheltucker commentedon Apr 22, 2024
Ok I updated to
supabase_flutter: ^2.5.1after seeing the 2.5.0 changelog description and I cannot reproduce the bug anymore.Could somebody confirm that that indeed fixed the custom LocalStorage implementation?
Update:
Another interesting thing I noticed with the update is that I do not see the app continuously refreshing the tokens in the background anymore.
Update 2:
Ok, was able to reproduce it again, but with a more informative error log.
dshukertjr commentedon Apr 22, 2024
@micheltucker Whether your implementation of the custom local storage works fine or not depends on the implementation itself. The error you were encountering makes me think that there is something wrong with the implementation, and the application is not able to update the refresh token with the new one whenever it's issued.
The update on v2.5.0 may have reduced how often token refresh happens, but I don't think it fixed the root cause of what you were having trouble with. All of that, however, is just speculation, and we cannot confirm anything unless you share your implementation of your custom local storage.
micheltucker commentedon Apr 22, 2024
Makes sense. I really just implemented your template code:
and because I need to share it with another part of my app I modified it ever so slightly, but it happens with either code:
micheltucker commentedon Apr 23, 2024
Ok so it must be something with the keychain implementation, as I now used this pub
import 'package:shared_preference_app_group/shared_preference_app_group.dart';and extended it like so:Would be great if somebody could look at the keychain implementation, as it is also the main example for the custom LocalStorage. I spent quite a bit of time trying to figure out where the issue is, but I now believe that the issue actually is with the Keychain/Supabase implementation.
sai-chandra22 commentedon May 26, 2025
This might resolve the issue, GoTrue by default treats any non‐retryable AuthException during the refresh as “session is dead, sign the user out.” The Supabase service is intentionally rotating your refresh tokens on every use, so if two refreshes race, the second one will come in with an already-used token, get back a 400 refresh_token_already_used, and GoTrue will immediately tear down the session.
The quickest fix is to treat that specific error as retryable, rather than a hard sign-out.
Vinzent03 commentedon May 27, 2025
@baptiste-veyrard You shouldn't share a session like that, as a session has only one refresh token and that is invalidated and regenerated for each new access token. So your issue is expected.
@sai-chandra22 There shouldn't be a case of two access token refreshes to happen simultaneously on the same client. And two different clients shouldn't share the same refresh token. This only happens if two different clients are working with the same refresh token, which should not be the case. But if the client gets a refresh_token_already_used error there is no way to recover the session, so a sign-out is the only way to handle it.
sai-chandra22 commentedon May 28, 2025
@Vinzent03 , But the cases of two access token is happening, we have give auto refresh capability to supabase sdk by the argument auto refresh to true, and not calling any manual refresh token anywhere still the refresh_token_already_used error and user is logging out
Vinzent03 commentedon May 28, 2025
@sai-chandra22 Are you even using a custom local storage as the original reason for this issue? May you be affected by #1164? So you are using
SharedPreferencesAsyncorSharedPreferencesWithCachefrom the new shared preferences api?sai-chandra22 commentedon May 29, 2025
This is what I am using as custom storage, on some devices it is working fine, on some devices the sign out is happening, when app is launched, indicating the same error we discussed above, so that's why I am asking if sharedPreferences can resolve the issue
github-actions commentedon Mar 8, 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 19, 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(gotrue): prevent stale token refresh from overwriting concurrent …