Session deleted from auth local storage while refreshing token due to "refresh token already used" when sharing auth local storage with multiple processes #703
Labels
bug
Something isn't working
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: