Persistent sign-in sounds like a convenience feature: save a token and skip the login screen next time. The harder part is deciding what the app knows when it starts.
It may have valid credentials, expired credentials that can be refreshed, a revoked session, or no network connection. Those are different states. Treating them all as “logged out” creates unnecessary friction and can erase the very information needed to recover.
I built session restoration in Flutter apps by loading a stored token, checking it with the backend, and retrieving the account data. Reviewing that flow exposed an important correction to the original example: a network failure should not cause the app to delete the token.
Store credentials deliberately
Access and refresh tokens are credentials. They should not live in ordinary preferences alongside theme settings. iOS provides Keychain services for sensitive items. On Android, the Keystore protects cryptographic keys; an application storage layer can use those keys to protect token data.
Configure the Flutter storage wrapper for the platform and account lifecycle, including backup behavior, reinstall, and account switching.
Persist session credentials instead of the user’s password, and keep server signing secrets on the server. Use HTTPS for transport; server-side session policy determines whether a credential is accepted.
Give startup more than two outcomes
A boolean isLoggedIn is often too small for startup. I prefer a state model that distinguishes restoring, signed in, signed out, and temporarily unable to verify.
This illustrative pseudocode focuses on that distinction:
Future<SessionState> restoreSession() async {
final credential = await secureStore.readSession();
if (credential == null) return SessionState.signedOut;
try {
final session = await authApi.restore(credential);
await secureStore.saveSession(session.credentials);
return SessionState.signedIn;
} on SessionRejected {
await secureStore.clearSession();
return SessionState.signedOut;
} on NetworkUnavailable {
return SessionState.unverifiedOffline;
}
}
The exception types represent application-defined categories. A real implementation must distinguish an explicit session rejection from a timeout, server failure, or permission error on a particular resource. Offline state must not grant access to operations that require fresh server authorization.
The UI can explain the difference: “Reconnect to verify your session” gives the user a more accurate next step than abruptly returning them to sign-in.
Coordinate refresh across requests
Several requests may discover an expired access token at once. Independent refreshes can race, especially when the server invalidates the old refresh token after issuing a replacement. RFC 9700 explains refresh-token rotation and replay detection for OAuth systems.
I would coordinate requests around one in-flight refresh operation. Waiting requests use its result, then retry only when appropriate. The refresh request itself must bypass the interceptor path that triggered it, or the app can enter a loop.
Retries should also be bounded. A successful refresh followed by another authorization rejection needs investigation or sign-out. Retrying a mutation requires separate attention to whether the server already applied it.
Make logout a complete transition
Logout clears local credentials and user-specific state. Where the authentication system supports it, the app should also request server-side revocation. If it is offline, clearing local state alone cannot revoke a credential already held elsewhere. Cached data and notification token associations also need attention when someone else may sign in on the same device.
A subtle case is logout during refresh. An old async response must not save credentials and sign the previous user back in. Tracking a session generation or cancelling obsolete work can protect that boundary.
Test the transitions people actually encounter
The useful scenarios include a cold launch online, launch offline, rejected refresh, simultaneous unauthorized responses, logout during a request, and switching accounts. I would also check that logs never contain credential values.
The hardest part of persistent sign-in is the transition between states. I want a returning user to keep their place when the network fails, while making clear which actions still require server authorization.