BaseGuard class abstract
Base Guard.
Provides common functionality for all guards:
- User state and caching
- Token management (with optional refresh token)
- Session restoration with cache-first strategy
Cache Strategy
On restore:
- Load user from cache (instant)
- Sync from API in background (fresh data)
Extending
class MyGuard extends BaseGuard {
MyGuard() : super(
userEndpoint: '/api/me',
userFactory: (data) => User.fromMap(data),
);
@override
Future<void> login(Map<String, dynamic> data, Authenticatable user) async {
await startSession(user, token: data['token'] as String?);
}
}
- Implemented types
- Implementers
Constructors
Properties
- cachedToken → String?
-
Cached token for sync access.
no setter
- guest → bool
-
Check if user is a guest (not authenticated).
no setteroverride
- hashCode → int
-
The hash code for this object.
no setterinherited
- refreshEndpoint → String?
-
API endpoint to refresh access token.
final
- refreshTokenKey → String?
-
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
stateNotifier
→ ValueNotifier<
int> -
Auth state notifier.
final
- tokenKey → String
-
Vault keys.
final
- userCacheKey → String
-
final
- userEndpoint → String?
-
API endpoint to fetch user data.
final
-
userFactory
→ Authenticatable Function(Map<
String, dynamic> )? -
Factory to create user from API response.
final
Methods
-
cacheUser(
Authenticatable user) → Future< void> - Cache user data to Vault.
-
check(
) → bool -
Check if user is authenticated.
override
-
clearTokens(
) → Future< void> - Clear all tokens.
-
clearUserCache(
) → Future< void> - Clear cached user.
-
extractUserData(
Map< String, dynamic> ? data) → Map<String, dynamic> ? - Extract user data from API response.
-
getRefreshToken(
) → Future< String?> - Get refresh token.
-
getToken(
) → Future< String?> -
Get the stored token.
override
-
hasToken(
) → Future< bool> -
Check if a token exists in storage.
override
-
id(
) → dynamic -
Get the authenticated user's ID.
override
-
loadCachedUser(
) → Future< Authenticatable?> - Load user from cache.
-
loadTokenToCache(
) → Future< void> - Load token into cache.
-
login(
Map< String, dynamic> data, Authenticatable user) → Future<void> -
Login with data and set the authenticated user.
inherited
-
logout(
) → Future< void> -
End the session.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
refreshToken(
) → Future< bool> -
Refresh the access token using the refresh token.
override
-
restore(
) → Future< void> -
Restore session from storage.
override
-
setUser(
Authenticatable user) → void -
Set the user directly.
override
-
startSession(
Authenticatable user, {String? token, String? refreshToken}) → Future< void> -
Open a session: persist
token(when given) and itsrefreshToken, then setuserand the in-memory token together, then cache the user. -
storeToken(
String token, [String? refreshToken]) → Future< void> - Store token (and optional refresh token).
-
toString(
) → String -
A string representation of this object.
inherited
-
user<
T extends Model> () → T? -
Get the authenticated user.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited