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
Session deleted from auth local storage while refreshing token due to "refresh token already used" when sharing auth local storage with multiple processes
#703
New issue
Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
When multiple processes share the same auth local storage (keychain in an app group, for example) and they try to refresh the token at the same time, one of them succeeds, and the others fail since the refresh token was already used, then the SDK deletes the token from the keychain.
This issue happens with any configuration when there are multiple processes involved sharing the same auth local storage and they try to refresh the token at the same time. This is hard to reproduce, but eventually the configuration below will reproduce the issue.
Create a custom auth local storage with a keychain in an app group.
Run different processes using the shared keychain. For example, a single app and an app extension, or two apps. Whatever combination of different processes using the same keychain. In my case it was an app, a widget and app intents.
Expected behavior
It is expected that the SDK doesn't delete valid tokens from the keychain.
Proposed Fix
My fix proposal is to introduce a check before deleting tokens. If the error that was thrown in the api.execute call of LiveSessionManager.refreshsession(_ refreshToken:) is AuthError.api where errorCode is "refresh_token_already_used", then extract the current session from storage, if the session is not expired, do nothing.
paulofaria
changed the title
Session deleted from keychain due to "refresh token already used" when sharing keychain with multiple processes
Session deleted from keychain due to "refresh token already used" when sharing auth local storage with multiple processes
Apr 9, 2025
paulofaria
changed the title
Session deleted from keychain due to "refresh token already used" when sharing auth local storage with multiple processes
Session deleted from auth local storage due to "refresh token already used" when sharing auth local storage with multiple processes
Apr 9, 2025
paulofaria
changed the title
Session deleted from auth local storage due to "refresh token already used" when sharing auth local storage with multiple processes
Session deleted from auth local storage when refreshing token due to "refresh token already used" when sharing auth local storage with multiple processes
Apr 9, 2025
paulofaria
changed the title
Session deleted from auth local storage when refreshing token due to "refresh token already used" when sharing auth local storage with multiple processes
Session deleted from auth local storage while refreshing token due to "refresh token already used" when sharing auth local storage with multiple processes
Apr 9, 2025
Chiming in about the business impact -- this was a particularly nasty bug for us. As soon as we added a widget to our app, users constantly complained about getting randomly logged out, and it took us a long time to track this down. More than a few users ragequit over this.
Bug report
Describe the bug
When multiple processes share the same auth local storage (keychain in an app group, for example) and they try to refresh the token at the same time, one of them succeeds, and the others fail since the refresh token was already used, then the SDK deletes the token from the keychain.
It is similar to what was reported here:
#486
To Reproduce
This issue happens with any configuration when there are multiple processes involved sharing the same auth local storage and they try to refresh the token at the same time. This is hard to reproduce, but eventually the configuration below will reproduce the issue.
Expected behavior
It is expected that the SDK doesn't delete valid tokens from the keychain.
Proposed Fix
My fix proposal is to introduce a check before deleting tokens. If the error that was thrown in the
api.execute
call ofLiveSessionManager.refreshsession(_ refreshToken:)
isAuthError.api
whereerrorCode
is"refresh_token_already_used"
, then extract the current session from storage, if the session is not expired, do nothing.supabase-swift/Sources/Auth/Internal/SessionManager.swift
Lines 100 to 116 in 5c06db6
Another approach is to not even check if the error is
"refresh_token_already_used"
, maybe just never delete valid tokens in:supabase-swift/Sources/Auth/Internal/SessionManager.swift
Line 130 in 5c06db6
Screenshots
Not applicable.
System information
Additional context
No additional context.
The text was updated successfully, but these errors were encountered: