Concepts

Sessions and tokens

AuthSession, SessionKey, AuthUser and AuthTokens, and the rule that keeps identity credentials out of session tokens.

AuthSession

One signed-in identity on the device.

FieldMeaning
keyA SessionKey: the provider id plus the user id within that provider.
userAn AuthUser.
tokensThe application tokens, or null for an identity-only session.
providerDataProvider data that is not a session token, JSON-encodable.
createdAt, lastUsedAtWhen the session was created on this device, and last signed in to or switched to.

Read the active session with authyra.currentSession and all of them, most recently used first, with authyra.sessions.

SessionKey

SessionKey('api', '42') identifies the user 42 of the provider api. User ids are only unique within one provider: 42 in your API and 42 in an OAuth server are different people. Signing in again with the same key updates the existing session instead of adding a duplicate.

Keep provider ids stable across releases: persisted sessions refer to them.

AuthUser

const AuthUser({
  required String id,
  String? email,
  String? name,
  String? avatarUrl,
  Map<String, Object?> claims = const {},
});

Anything else your backend returns goes into claims.

AuthTokens

AuthTokens.expiringIn(
  Duration(seconds: json['expires_in'] as int),
  accessToken: json['access_token'] as String,
  refreshToken: json['refresh_token'] as String?,
);
FieldMeaning
accessTokenThe bearer token your API accepts.
refreshTokenUsed to obtain new tokens. Without it, the session ends when the access token expires.
accessTokenExpiresAtWhen the access token expires. Leave it null if your backend does not say: Authyra then relies on 401 responses rather than guessing.
refreshTokenExpiresAtWhen the refresh token expires, if your backend says. Authyra refreshes before that deadline to keep the session alive.

When a refresh response omits the refresh token, the current one is kept: backends that do not rotate refresh tokens work unchanged.

toString() never prints token values.

Session tokens, not identity credentials

AuthSession.tokens holds application session tokens: the ones your API accepts and can renew. A credential that only proves an identity and still needs exchanging belongs in providerData:

  • a Google ID token,
  • an Apple identity token or authorization code,
  • a GitHub access token your API does not accept.

The native providers follow this rule. GoogleProvider() and AppleProvider() return an identity-only session: no tokens, the credential in providerData, for example to hand to Firebase. GoogleProvider.withExchange and AppleProvider.withExchange send the credential to your backend, which returns your own tokens.

Authyra never refreshes an identity provider's own tokens. Renewing them needs secrets that must stay on a server.

Identity-only sessions

A session without tokens is valid: the user is signed in, and accessToken() returns null. It never expires on its own and is never refreshed; it ends when you sign out.

Copyright © 2026