Guard class abstract
The Guard Contract.
Guards manage authentication state for the application. This is a minimal, frontend-focused interface.
Built-in Guards
- BearerTokenGuard: Token-based auth (most common)
- BasicAuthGuard: HTTP Basic authentication
- ApiKeyGuard: API key authentication
Usage
// Login (app handles API call, passes result)
final response = await Http.post('/login', data: credentials);
final user = User.fromMap(response['data']['user']);
await Auth.login({'token': response['data']['token']}, user);
// Check auth state
if (Auth.check()) {
print('Logged in as ${Auth.user<User>()?.name}');
}
// Logout
await Auth.logout();
- Implementers
Constructors
- Guard()
Properties
- guest → bool
-
Check if user is a guest (not authenticated).
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
stateNotifier
→ ValueNotifier<
int> -
Notifier that bumps on every auth state change.
no setter
Methods
-
check(
) → bool - Check if user is authenticated.
-
getToken(
) → Future< String?> - Get the stored token.
-
hasToken(
) → Future< bool> - Check if a token exists in storage.
-
id(
) → dynamic - Get the authenticated user's ID.
-
login(
Map< String, dynamic> data, Authenticatable user) → Future<void> - Login with data and set the authenticated user.
-
logout(
) → Future< void> - Log the user out.
-
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.
-
restore(
) → Future< void> - Restore session from storage.
-
setUser(
Authenticatable user) → void - Set the user directly.
-
toString(
) → String -
A string representation of this object.
inherited
-
user<
T extends Model> () → T? - Get the authenticated user.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited