API Reference

AccountManager

Complete API reference for AccountManager, the multi-account facade.

Overview

AccountManager is the consumer-facing facade over SessionManager's multi-account registry. Access it via Authyra.instance.accounts or client.accounts; don't construct it directly.

final accounts = Authyra.instance.accounts;

Account information

Future<List<AuthUser>> getAll()            // sorted by most recently used
Future<List<AuthSession>> getAllSessions() // same order, full sessions with tokens
int get count                              // total registered accounts, including expired
bool get hasMultiple                       // count > 1
AuthUser? get active                       // the currently active user, or null
final users = await accounts.getAll();
if (accounts.hasMultiple) showAccountSwitcher();

Switching

switchTo()

Future<void> switchTo(String userId)

Marks userId's session as used and makes it active. Notifies the reactive layer with AuthState.authenticated(user).

Throws: AccountNotFoundException if userId has no registered session, TokenExpiredException if that session has expired, AuthenticationFailedException if an AuthCallbacks.onBeforeAccountSwitch override denies it.

Emits: AccountSwitchEvent on the client's event bus when the active user actually changes.

try {
  await accounts.switchTo(userId);
} on AccountNotFoundException {
  showError('Account not found');
} on TokenExpiredException {
  showError('Session expired, please sign in again');
}

isValid()

Future<bool> isValid(String userId)

true if userId has a registered, non-expired session. Check before switchTo if you'd rather branch than catch an exception.

getTokenFor()

Future<String?> getTokenFor(String userId)

Access token for userId without switching the active account. Useful for a background sync call on behalf of a non-active account.


Removal

signOut()

Future<void> signOut(String userId)

Removes userId's session. If it was the active account, the most recently used remaining account (if any) becomes active; otherwise the reactive layer emits AuthState.unauthenticated(). A no-op if userId isn't registered.

Throws: AuthenticationFailedException if an AuthCallbacks.onBeforeAccountRemove override denies it.

Emits: AccountRemovedEvent.

signOutAll()

Future<void> signOutAll()

Clears every registered session and emits AuthState.unauthenticated(). Not individually gated by onBeforeAccountRemove (that hook is per-account).


Maintenance

updateSession()

Future<void> updateSession(String userId, AuthSession session)

Replaces the stored session for userId. Typically called by the provider layer after an out-of-band token refresh, or when server-side profile data changes.

cleanExpired()

Future<int> cleanExpired()

Removes every expired session and returns the count removed. Called automatically during SessionManager.initialize(); call it again manually on app-foreground events if you want. Never throws, errors are logged and swallowed, returning 0.


Callbacks and events

AccountManager honours the same AuthCallbacks and AuthEventBus instance passed to AuthyraClient:

class MyCallbacks extends AuthCallbacks {
  @override
  Future<CallbackResult> onBeforeAccountSwitch(AuthUser fromUser, String toUserId) async {
    return const CallbackResult.allow();
  }

  @override
  Future<CallbackResult> onBeforeAccountRemove(AuthUser user) async {
    return const CallbackResult.allow();
  }
}

client.events.on<AccountSwitchEvent>((e) {
  analytics.track('account_switch', {'to': e.toUser.id});
});
client.events.on<AccountRemovedEvent>((e) {
  analytics.track('account_removed', {'user': e.userId});
});

See Callbacks → and Events →.


See also

Copyright © 2026