SessionManager class

Ends a user session after a period of inactivity.

final session = kit.createSessionManager(
  timeout: const Duration(minutes: 5),
);
session.onSessionExpired.listen((_) => goToLogin());
session.startSession();
// on every user interaction:
session.recordActivity();

Expiry is decided from the wall clock, not only from the timer, so a session that timed out while the app was suspended expires as soon as the app runs again (on the next timer tick or recordActivity call).

Constructors

SessionManager({required Duration timeout, SecureTokenStorage? tokenStorage, TokenExpiryManager? tokenExpiry, DateTime now()?, bool clearTokensOnExpiry = true})
Throws ArgumentError when timeout is zero or negative.

Properties

clearTokensOnExpiry → bool
Whether stored tokens are removed when the session times out.
final
hashCode → int
The hash code for this object.
no setterinherited
isActive → bool
Whether a session is running and has not timed out.
no setter
onSessionExpired → Stream<void>
Fires once each time the session ends because of inactivity (after the tokens were cleared). Not fired by endSession.
no setter
remainingTime → Duration
Time left until the session expires; zero when there is no session.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
timeout → Duration
Inactivity period after which the session expires.
final

Methods

dispose() → Future<void>
Stops the timer and closes onSessionExpired. Tokens are kept.
endSession({bool clearTokens = true}) → Future<SecurityResult<void>>
Ends the session explicitly (logout). Does not emit onSessionExpired. With clearTokens the stored tokens and expiry are removed; the first failure of that cleanup is returned.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
recordActivity() → bool
Call on user interaction to push the expiry back.
startSession() → void
Starts (or restarts) the session and its inactivity countdown.
toString() → String
A string representation of this object.
inherited

Operators

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