Concepts

Token refresh

When Authyra refreshes, why there is never more than one refresh per session, and how failures are handled.

When a refresh happens

TriggerApplies toDetails
ScheduledThe active sessionrefreshThreshold (60 s by default) before the access token expires. Disable with AuthyraConfig(autoRefresh: false).
On demandAny sessionaccessToken() refreshes first when the token is within refreshThreshold of its expiry.
After a 401Any sessionaccessToken(rejectedToken: token) refreshes even if the token looks valid. authyra_dio does this for you.
Back in the foregroundThe active sessionrefreshIfNeeded(), called by AuthyraScope: timers do not run while a mobile app is suspended.
Refresh token deadlineThe active sessionWhen your backend gives refreshTokenExpiresAt and refreshing extends it, Authyra refreshes before that deadline so an idle app keeps its session.
ExplicitlyAny sessionauthyra.refresh() refreshes now, for example after changing the user's role server-side.

One refresh at a time

Every trigger goes through one coordinator, which runs at most one refresh per session:

  • Callers that need a refresh while one is in flight wait for it and get its result.
  • The refresh always uses the latest refresh token.
  • A caller holding a token that was already replaced gets the new token without a second refresh.

That is what makes rotating refresh tokens safe. A server that issues a new refresh token on every refresh, and treats reuse of an old one as theft, never sees the same token twice from your app.

request A ─ 401 ─┐
request B ─ 401 ─┼─▶ one refresh ─▶ new tokens ─▶ A, B and C retry
request C ─ 401 ─┘

What ends a session

A session that can be refreshed ends in one case only: the provider throws RefreshRevokedException, meaning the server definitively rejected the refresh token. The session is removed with SessionEndReason.revoked.

A session that cannot be refreshed (its provider does not support refresh, it holds no refresh token, or its refresh token has expired) ends as soon as its access token is unusable:

  • when the server rejects it (a 401 handled by AuthyraInterceptor, or accessToken(rejectedToken:)), with SessionEndReason.revoked;
  • when it expires, with SessionEndReason.expired: at startup, on a scheduled check, on accessToken() or on switchTo.

For a session that can be refreshed, every other failure is transient and keeps the session:

  • network errors and timeouts,
  • 5xx responses,
  • a 403 from a proxy or firewall,
  • anything else your provider throws.

A failed scheduled refresh emits SessionRefreshFailedEvent and is retried with exponential backoff: retryDelay (5 s by default), doubling up to maxRetryDelay (5 min). If the access token is still valid, accessToken() returns it; once it has expired, accessToken() throws TokenRefreshException and the session waits for the network to return.

Map responses carefully in your provider. A refresh endpoint answering 401 or invalid_grant means revoked. A 500, a timeout or a captive portal page does not.

Rotated tokens and storage

New tokens are persisted before they are used. If the write fails (a full disk, a locked keychain), the new tokens are kept in memory anyway, because the old refresh token is already consumed on the server, and written again with backoff until it succeeds. dispose() waits for that write.

Configuration

const AuthyraConfig(
  autoRefresh: true,                          // Scheduled refresh of the active session.
  refreshThreshold: Duration(seconds: 60),    // How early a token is considered due.
  retryDelay: Duration(seconds: 5),           // First retry after a transient failure.
  maxRetryDelay: Duration(minutes: 5),        // Backoff ceiling.
);

Invalid values, such as a zero retryDelay that would spin while offline, make the Authyra constructor throw AuthConfigException.

Copyright © 2026