API Reference

AuthState

Complete API reference for AuthState, the discriminated union emitted by authStateChanges.

Overview

AuthState is a small value object with three mutually exclusive states: authenticated, unauthenticated, or error. It's the type emitted by authStateChanges.

class AuthState extends Equatable {
  final AuthStateType type;
  final AuthUser? user;   // non-null only when type == authenticated
  final String? error;    // non-null only when type == error
}

enum AuthStateType { authenticated, unauthenticated, error }

Because AuthState extends Equatable, identical consecutive states are deduplicated on the stream, no spurious StreamBuilder rebuilds when nothing actually changed.


Factories

AuthState has no public constructor; build one of these instead:

factory AuthState.authenticated(AuthUser user)
factory AuthState.unauthenticated()
factory AuthState.error(String error)
final state = AuthState.authenticated(currentUser);
assert(state.isAuthenticated);
assert(state.user != null);

Accessors

bool get isAuthenticated;    // type == AuthStateType.authenticated
bool get isUnauthenticated;  // type == AuthStateType.unauthenticated
bool get hasError;           // type == AuthStateType.error

Pattern matching

switch (state.type) {
  case AuthStateType.authenticated:
    return DashboardPage(user: state.user!);
  case AuthStateType.unauthenticated:
    return const LoginPage();
  case AuthStateType.error:
    return ErrorPage(message: state.error!);
}

When each state is emitted

TriggerResulting state
initialize() restores a persisted sessionauthenticated(user)
initialize() finds no sessionunauthenticated()
signIn() succeedsauthenticated(user)
signOut() completesunauthenticated(), or authenticated(nextUser) if another account remains
Token refresh succeedsauthenticated(user) (same user, fresh token)
Token refresh is exhausted or deniedunauthenticated()
signIn() failserror(message)
Copyright © 2026