You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.4 KiB
Dart

/// Lightweight **in-memory** authentication token holder.
///
/// The token is **never persisted to disk** — it lives only for the duration
/// of the current process. Use [setToken] after a successful login response
/// and [clearToken] on log-out.
///
/// [ApiClient] reads from here automatically whenever `shouldKeepToken: true`
/// is passed to any call method, and updates the stored token when the server
/// returns a new `Authorization` response header.
///
/// Example:
/// ```dart
/// // After a login call:
/// TokenManager.setToken(loginResponse.accessToken);
///
/// // Any subsequent call with shouldKeepToken: true will attach:
/// // Authorization: Bearer <token>
///
/// // On log-out:
/// TokenManager.clearToken();
/// ```
final class TokenManager {
// Pure-static class — disallow instantiation.
TokenManager._();
static String? _token;
/// Store an authentication token in memory.
///
/// Pass the **raw token value** (without a `Bearer ` prefix).
static void setToken(String token) => _token = token;
/// Retrieve the currently stored token, or `null` if none has been set yet.
static String? getToken() => _token;
/// Wipe the stored token (e.g. on log-out or session expiry).
static void clearToken() => _token = null;
/// `true` when a non-empty token is currently held in memory.
static bool get hasToken => _token != null && _token!.isNotEmpty;
}