AuthManager class

Authentication manager for handling user authentication

This class acts as a facade for authentication operations, providing a unified interface for different authentication guards (web, api, etc.). It implements the Strategy pattern and Factory pattern for better maintainability and extensibility.

Features:

  • Multi-guard support (web, api, etc.)
  • Provider-specific authentication
  • Configuration-driven guard selection
  • Unified authentication interface
  • Guard caching for performance
  • Extensible guard registration

Example usage:

// Using default guard and provider
final authManager = AuthManager();
final result = await authManager.attempt({'email': 'user@example.com', 'password': 'password'});

// Using specific guard
final webAuth = AuthManager(guard: 'web');
final webResult = await webAuth.attempt(credentials);

// Using specific guard and provider
final adminAuth = AuthManager(guard: 'web', provider: 'admins_web');
final adminResult = await adminAuth.attempt(credentials);

// Verifying tokens
final user = await authManager.user(token);

// Logging out
await authManager.logout(token);

Constructors

AuthManager({String? guard, String? provider, AuthConfig? authConfig})
Creates an enhanced authentication manager instance

Properties

config AuthConfig
Gets the authentication configuration
no setter
guard String
Gets the current authentication guard name
no setter
guardInstance Guard
Gets the current authentication guard
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

Methods

attempt(Map<String, dynamic> credentials) Future<AuthResponse>
Attempts to authenticate a user with the provided credentials
attemptWithGuard(String guardName, Map<String, dynamic> credentials) Future<AuthResponse>
This method provides secure token refresh functionality with:
check(String token) Future<bool>
Checks if a token is valid
getAvailableGuards() List<String>
Gets a list of available guards
getGuard(String guardName) Guard
Gets a guard instance by name
logout(String token) Future<void>
Logs out the user with the current guard.
logoutAll(dynamic token) Future<void>
Logs out the current user from all devices
logoutAllWithGuard(String guardName, dynamic token) Future<void>
Logs out the current user from all devices with a specific guard
logoutOthers(dynamic userId, String currentToken) Future<void>
Logs out the user from other devices (keeps current session)
logoutWithGuard(String guardName, String token) Future<void>
Logs out with a specific guard
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
refresh(String refreshToken) Future<AuthResponse>
Refreshes an access token using a refresh token
toString() String
A string representation of this object.
inherited
user(String token) Future<Authenticatable>
Verifies the validity of an authentication token
userWithGuard(String guardName, String token) Future<Authenticatable>
Verifies a token with a specific guard

Operators

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

Static Methods

clearGuardCache() → void
Clears the guard cache
registerGuardFactory(String guardName, Guard factory(AuthConfig config, String guardName)) → void
Registers a custom guard factory