TokenManager<T> class Getting Started Streams

Manages the tokens of one session: storage, single-flight refresh and an observable state.

Refresh is on demand: tokens are refreshed when validToken, authorize, refreshIfNeeded or a rejected token asks for it, and, when refreshAhead is set, in the background just before they expire. There is no timer. Concurrent callers share one refresh.

final manager = TokenManager<TokenSet>.create(
  store: InMemoryTokenStore<TokenSet>(),
  refresher: MyRefresher(),
  info: const TokenSetInfo(),
);
await manager.setTokens(tokens);
final valid = await manager.validToken();

Constructors

TokenManager({required TokenStorage<T> storage, TokenManagerRefreshDelegate<T>? refreshDelegate, TokenInfoDelegate<T>? infoDelegate})
Creates a manager from the old storage and delegates.
TokenManager.create({required TokenStore<T> store, TokenRefresher<T>? refresher, TokenInfo<T>? info, Duration skew = const Duration(seconds: 30), Duration? refreshAhead, Clock clock = const Clock()})
Creates a manager over store.

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
states → Stream<AuthState<T>>
Emits the current state on listen, then every change.
no setter
tokens → T?
Returns the latest tokens, or null before they are loaded.
no setter
tokensStream → Stream<T?>
Emits the tokens on every change; null means signed out.
no setter
waitForFresh → Future<void>?
Completes when the refresh in flight ends; null when none is running.
no setter

Methods

authorize<R>(Future<R> send(T tokens), {required bool isAuthFailure(Object error)}) → Future<R>
Runs send with valid tokens, replaying it once after an auth failure.
authorizeStream<R>(Stream<R> open(T tokens), {required bool isAuthFailure(Object error)}) → Stream<R>
Opens a stream with valid tokens, reopening it once after an early auth failure. Pause, resume and cancel reach the source stream.
current() → Future<T?>
Returns the current tokens without refreshing them.
dispose() → Future<void>
Releases the manager; work that finishes later is discarded.
executeRefresh() → Future<void>
Forces a refresh; failures that are exceptions are swallowed.
initialize() → Future<void>
Loads the stored tokens.
logout() → Future<void>
Removes the tokens and signs the session out.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
refreshIfNeeded({Duration? within}) → Future<void>
Refreshes the tokens when less than within is left before they expire.
setTokens(T? tokens) → Future<void>
Stores tokens; null signs out like logout.
toString() → String
A string representation of this object.
inherited
validToken({T? rejected}) → Future<T>
Returns tokens that are valid now, refreshing them when needed.

Operators

operator ==(Object other) → bool
The equality operator.
inherited