Skip to content
All writing
Backend15 Aug 20263 min read

Authentication between a Flutter app and a backend API

Token storage, silent refresh, and the race condition everyone hits the first time.

Login is the first feature in most apps and the one most often built twice, because the first version works fine until tokens start expiring in real usage.

Where tokens go

Not SharedPreferences. It is plain, readable storage.

flutter_secure_storage, which is Keychain on iOS and EncryptedSharedPreferences on Android. That is the baseline, and it is a two-line change from the wrong answer.

The refresh token is the one that matters. An access token leaking is bad for fifteen minutes; a refresh token leaking is bad until someone notices.

Short access token, long refresh token

Access token: 15 minutes, sent on every request. Refresh token: long-lived, only ever sent to /auth/refresh.

Short access tokens mean you do not need a real-time revocation check on every request. Someone who steals one gets a small window. Logging out is handled by revoking the refresh token server-side.

The race condition

Your app opens. Four requests fire at once for the home screen. The access token is expired. All four get a 401. All four try to refresh.

Now you have four refresh calls with the same token. If your backend rotates refresh tokens, and it should, then three of them are using a token that was just invalidated, and the user gets logged out on app open. Intermittently. Which is the worst kind of bug to be handed.

The fix is one shared refresh:

dart
Future<String>? _refreshing;
 
Future<String> getValidToken() {
  final token = _storage.accessToken;
  if (token != null && !token.isExpired) return Future.value(token.value);
 
  // Everyone waits on the same refresh call.
  return _refreshing ??= _performRefresh().whenComplete(() => _refreshing = null);
}

One in-flight refresh, everyone else awaits it. In an interceptor, also queue the failed requests and replay them once the new token arrives, so the user does not see four error states flash past.

Refresh token rotation

Every refresh returns a new refresh token and invalidates the old one. If an old one is ever used again, that is a signal the token was stolen. Revoke the whole family and force a login.

This is a backend decision, but the client has to handle it properly or the rotation just creates the logout bug above.

What the app does at startup

  1. Read the tokens from secure storage.
  2. If there is no refresh token, go to login. Do not call the API.
  3. If there is one, refresh in the background and show the app.

Do not block the splash screen on a network call. On a bad connection that is ten seconds of staring at a logo, and the user has an offline-capable home screen sitting right there.

Logout

Clear secure storage, call the revoke endpoint, and reset in-memory state. That last part is the one that gets missed. A cached user object or an open stream survives the logout and the next user sees the previous one's data for a second. It is a small bug that looks very bad.

Biometrics

Biometric unlock does not replace the token. It gates access to the stored refresh token locally. The server never knows a fingerprint was involved, and it should not.

What I would ask in a review

  • Where is the refresh token stored?
  • What happens when two requests get a 401 at the same time?
  • What happens when refresh itself fails?
  • Does logout clear in-memory state as well as storage?
  • Is the token ever written to a log?

That last one catches more real problems than the other four together.