Concepts

State and events

The sealed AuthState your UI renders, the events that describe what happened to each session, and the errors Authyra throws.

AuthState

Three states, as a sealed class your code can switch over exhaustively:

StateMeaningShow
AuthUnknownSaved sessions are being restored.A splash screen. Showing the sign-in screen here makes it flash for signed-in users.
AuthSignedIn(key, user)A session is active.The app.
AuthSignedOutNo session is active.The sign-in screen.
final screen = switch (authyra.state) {
  AuthUnknown() => const SplashScreen(),
  AuthSignedIn(:final user) => HomeScreen(user: user),
  AuthSignedOut() => const SignInScreen(),
};

authyra.states is a stream that gives each new listener the current state first, then every change. Equal consecutive states are not repeated.

AuthSignedIn carries the session's identity, never its tokens. A token refresh is not a state change, so your widgets do not rebuild every 15 minutes. Ask accessToken() when you need a token.

Errors are not states either: a failed sign-in throws to its caller and leaves the state as it was.

Events

authyra.events describes what happens to every session, active or not. Use it for analytics, audit logs, banners and debugging.

EventWhen
SessionStartedEvent(session, isNew)A sign-in succeeded. isNew is false when an existing session of the same identity was updated.
SessionSwitchedEvent(previous, current)The active session changed.
SessionRefreshedEvent(session)Tokens were renewed.
SessionRefreshFailedEvent(session, error)A refresh failed for a transient reason; it will be retried.
SessionEndedEvent(session, reason)A session was removed: signedOut, revoked by the server, or expired (could no longer be renewed).
SignInFailedEvent(providerId, error)A sign-in failed or was cancelled.
authyra.events.listen((event) {
  if (event case SessionEndedEvent(reason: SessionEndReason.revoked)) {
    showBanner('Your session ended. Please sign in again.');
  }
});

No event prints token values in its toString().

Errors

Every error Authyra throws is an AuthException, a sealed hierarchy. Its message never contains tokens.

ExceptionThrown when
SignInCancelledExceptionThe user closed the browser or dismissed the native sheet. Usually not worth a message.
SignInFailedExceptionThe provider could not authenticate the user: wrong password, server error, misconfiguration.
AuthDeniedExceptionYour AuthPolicy refused the operation. reason is meant for display.
SessionNotFoundExceptionswitchTo targeted a session that no longer exists or can no longer be renewed (it is then ended).
TokenRefreshExceptionA refresh failed for a transient reason where a fresh token was required: the current token expired, the server rejected it (accessToken(rejectedToken:)), or you called refresh(). The session is kept.
AuthStorageExceptionThe storage could not read or write.
AuthConfigExceptionMisuse: unknown provider id, duplicate ids, use before start().
RefreshRevokedExceptionThrown by providers, not by Authyra, to say the refresh token was rejected.
try {
  await authyra.signIn('api', params: credentials);
} on SignInCancelledException {
  // Nothing to say.
} on AuthException catch (error) {
  showError(error.message);
}
Copyright © 2026