Concepts

How it works

The roles of providers, Authyra, storage and your HTTP client, and what happens from sign-in to sign-out.

Four roles

  Your UI ──── reads ────▶ AuthState (AuthUnknown | AuthSignedIn | AuthSignedOut)
     │                          ▲
     │ signIn / signOut         │ publishes
     ▼                          │
  Authyra ── owns ──▶ sessions, refresh scheduling, multi-account, events
     │        │
     │        └── persists ──▶ AuthStorage (platform keystore)
     │
     └── asks ──▶ AuthProvider (your API, OAuth server, Google, Apple...)

  Your HTTP client ── accessToken() ──▶ Authyra
RoleResponsibilityHolds state?
ProviderAuthenticates the user, renews tokens, revokes them on sign-out.No. It returns results; it never stores a session.
AuthyraBuilds the session, persists it, refreshes it, ends it, publishes the state.Yes, the single owner of every session.
StorageKeeps sessions across restarts.Only strings under authyra.* keys.
HTTP clientAsks for a token before each request, reports a 401.No. authyra_dio does this for Dio.

Because providers are stateless, you can change how users sign in without touching how sessions behave, and test each side on its own.

The life of a session

  1. Sign-in. authyra.signIn('api', params: ...) calls the provider, which returns an AuthSignInResult: the user, the tokens, and optional provider data. Authyra builds an AuthSession, persists it, makes it active and emits AuthSignedIn.
  2. Use. Your HTTP client calls authyra.accessToken() before each request. A valid token is returned at once; a token about to expire is refreshed first.
  3. Proactive refresh. While the app runs, the active session is refreshed shortly before its access token expires (60 seconds by default), so requests rarely wait.
  4. Recovery. When your API answers 401 anyway, the client asks for a new token with rejectedToken. If another caller already refreshed, the new token is returned without a second refresh.
  5. Failures. A refresh that fails for a transient reason (network, timeout, 5xx) keeps the session and is retried with backoff. A refresh token the server rejects ends the session: Authyra emits SessionEndedEvent with SessionEndReason.revoked, and the state becomes AuthSignedOut if no other account remains.
  6. Sign-out. authyra.signOut() blocks new refreshes, waits for one in flight, asks the provider to revoke the latest tokens server-side, then removes the session.
  7. Restart. At the next launch, start() restores every session. A session whose access token expired while the app was closed is refreshed; one that can no longer be renewed is discarded.

One instance, or several

Authyra.initialize(...) creates an app-wide instance reachable through Authyra.instance. When you inject dependencies yourself, create one with the constructor and call start():

final authyra = Authyra(storage: SecureAuthStorage(), providers: [apiProvider]);
await authyra.start();

dispose() stops background work and waits for writes in progress. Sessions stay persisted.

Copyright © 2026