Multiple accounts
Every successful sign-in with a new identity adds a session. One session is active: it drives AuthState and is the default for accessToken() and authyra_dio.
Adding an account
// Becomes active (the default).
await authyra.signIn('api', params: workCredentials);
// Added without switching to it.
await authyra.signIn('api', params: personalCredentials, activate: false);
Signing in again with an identity that already has a session updates that session instead of creating a duplicate.
Listing and switching
for (final session in authyra.sessions) { // Most recently used first.
print('${session.user.email} active: ${session.key == authyra.currentSession?.key}');
}
await authyra.switchTo(authyra.sessions.last.key);
switchTo emits SessionSwitchedEvent and a new AuthSignedIn. A session whose access token expired in the meantime does not need a new sign-in when it can be refreshed: it is refreshed on its next use. switchTo throws SessionNotFoundException if the session was removed meanwhile, or if it can no longer be renewed (it is then ended with SessionEndReason.expired).
Calling the API as another account
The active account is only a default. Any request can target another one:
await api.get('/inbox', options: Options(extra: {AuthyraInterceptor.sessionKey: other.key}));
final token = await authyra.accessToken(key: other.key);
Each account keeps its own tokens. Inactive sessions are refreshed on demand, when a request needs them; only the active one is refreshed on a schedule.
Signing out
await authyra.signOut(); // The active account. The most recently used remaining one becomes active.
await authyra.signOut(key: other.key); // A specific account.
await authyra.signOutAll(); // Every account, one state change.
Restricting accounts
To allow a single account, or only accounts from one domain, use an AuthPolicy.