React Native Secure Token Storage: Proactive vs Reactive Refresh Decision
0 reputation · 23 May 2022, 07:38 UTC
0 reputation · 23 May 2022, 07:38 UTC
Implement least‑privilege authentication in a React Native app by storing short‑lived access tokens in memory and refresh tokens in the iOS Keychain or Android Keystore, avoiding AsyncStorage.
The missing decision is whether token refresh should be proactive—invoked before the access token expires using the expires_in value—or reactive—triggered only upon receiving a 401 from the API.
34775 reputation · 23 May 2022, 13:32 UTC
For most React Native applications, a reactive refresh strategy is the superior choice. Relying on a 401 Unauthorized response to trigger the token exchange minimizes unnecessary network overhead and avoids the complexity of managing timers across app lifecycle states (foreground/background), which can lead to battery drain and memory leaks.
While proactive refresh (using expires_in) can theoretically eliminate the latency of a failed request, the trade-offs in a mobile environment are generally unfavorable:
setTimeout or background tasks in React Native to align with token expiry is fragile and often interrupted by the OS.The primary risk of a reactive strategy is the "thundering herd" problem: multiple concurrent API calls failing simultaneously and each triggering a separate refresh request. To solve this, implement a Promise-based Singleton in your API interceptor:
let refreshTokenPromise = null;).refreshTokenPromise exists.This ensures that regardless of how many requests fail, only one exchange occurs with the authorization server.
When the refresh token itself is revoked or expired (e.g., the refresh endpoint returns a 400 or 401), the application must prioritize security over convenience. The safest UX path is:
To verify this implementation, perform the following tests:
/refresh call is dispatched.Note: This recommendation assumes the use of a secure storage wrapper (such as react-native-keychain) rather than AsyncStorage for the refresh token.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.