Guides

Sign-out and revocation

What happens when the user signs out, and when the server ends a session.

Signing out

await authyra.signOut();

In order:

  1. New refreshes of the session are blocked.
  2. A refresh already in flight is awaited, up to 10 seconds, so the newest tokens are the ones revoked.
  3. The provider's signOut revokes the session server-side. If that call fails (offline, server error), the failure is logged and the sign-out continues.
  4. The session is removed from storage, then from memory.
  5. SessionEndedEvent(reason: signedOut) is emitted, and the state changes.

If the same user signs in again while step 3 is still running, the new session is kept: the late sign-out does not remove it.

signOut never asks your AuthPolicy: a user can always sign out.

When the server ends the session

A session ends without the user asking when its refresh token is rejected: revoked by an admin, a password change, reuse detection, or simply expired. Authyra emits SessionEndedEvent(reason: revoked) and the state moves to the next account or to AuthSignedOut.

Tell the user why:

authyra.events.listen((event) {
  if (event case SessionEndedEvent(reason: SessionEndReason.revoked)) {
    showBanner('You were signed out. Please sign in again.');
  }
});

A session also ends with SessionEndReason.expired when it can no longer be renewed: its access token expired and it has no usable refresh token. This is detected at startup, on a scheduled check, on accessToken() or on switchTo.

Revoking on your backend

Implement the provider's sign-out so a stolen refresh token stops working:

CredentialsProvider(
  id: 'api',
  signIn: loginWithApi,
  refresh: refreshWithApi,
  signOut: (session) async {
    final refreshToken = session.tokens?.refreshToken;
    if (refreshToken != null) {
      await authApi.post<void>('/auth/sign-out', data: {'refresh_token': refreshToken});
    }
  },
)

OAuth2Provider calls the server's revocation endpoint when revocationEndpoint is set; BackendOAuthProvider posts to signOutEndpoint.

Wiping everything

await authyra.signOutAll();          // At runtime: revokes and removes every session.
await const SecureAuthStorage().deleteAll(); // Before start(): removes Authyra's keys only.
Copyright © 2026