SessionManager class final
Manages user authentication state across the application lifecycle.
Extends ChangeNotifier so widgets can rebuild reactively, and also exposes a stateStream for non-widget consumers (e.g. Bloc/Riverpod bridges).
State is persisted across restarts: on construction the manager restores the previous session from SecurePrefs and validates token expiry via TokenStore.
// In main()
final session = SessionManager.instance;
await session.restore();
// In a widget
final session = context.watch<SessionManager>();
if (session.isAuthenticated) { ... }
- Inheritance
-
- Object
- ChangeNotifier
- SessionManager
- Implemented types
Properties
- currentUserId → String?
-
The authenticated user's identifier, or
nullwhen unauthenticated.no setter - hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether any listeners are currently registered.
no setterinherited
- isAuthenticated → bool
-
Whether the user is currently authenticated.
no setteroverride
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- state → SessionState
-
The current session state.
no setteroverride
-
stateStream
→ Stream<
SessionState> -
A broadcast stream that emits every time state changes.
no setter
Methods
-
addListener(
VoidCallback listener) → void -
Register a closure to be called when the object changes.
inherited
-
dispose(
) → void -
Discards any resources used by the object. After this is called, the
object is not in a usable state and should be discarded (calls to
addListener will throw after the object is disposed).
override
-
login(
{required String accessToken, required String refreshToken, String? userId, Map< String, dynamic> ? userData}) → Future<void> - Persists tokens and transitions to SessionAuthenticated.
-
logout(
) → Future< void> - Clears all tokens, removes persisted session data, and transitions to SessionUnauthenticated.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyListeners(
) → void -
Call all the registered listeners.
inherited
-
removeListener(
VoidCallback listener) → void -
Remove a previously registered closure from the list of closures that are
notified when the object changes.
inherited
-
restore(
) → Future< void> - Restores session state from persisted storage.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- instance → SessionManager
-
Returns the singleton, constructing it with default dependencies on the
first call. Override dependencies only for testing via resetForTesting.
no setter
Static Methods
-
resetForTesting(
{required TokenStoreBase tokenStore, required SecurePrefsBase securePrefs}) → void -
Replaces the singleton with a test double. Call in test
setUp.