AuthHandler class
Handles JWT authentication, token storage, and automatic token refresh.
This class manages the complete authentication lifecycle including:
- Storing and retrieving JWT and refresh tokens securely
- Adding authorization headers to requests
- Refreshing expired tokens automatically
Example usage:
// Authorize a user after login
await authHandler.authorize(jwt: accessToken, refreshToken: refreshToken);
// Check if user is authorized
if (await authHandler.isAuthorized) {
// User has valid tokens stored
}
// Logout - clear all tokens
await authHandler.unAuthorize();
Constructors
- AuthHandler({required Dio dio, required RestApiClientOptions options, required AuthOptions authOptions, required ExceptionOptions exceptionOptions, required LoggingOptions loggingOptions, required ExceptionHandler exceptionHandler})
- Creates an AuthHandler instance.
Properties
- authOptions → AuthOptions
-
Authentication-specific options (refresh endpoint, token resolvers, etc.).
final
-
containsAuthorizationHeader
→ Future<
bool> -
Whether the Authorization header is currently set in Dio.
no setter
-
containsJwtInStorage
→ Future<
bool> -
Whether a JWT token exists in storage.
no setter
-
containsRefreshTokenInStorage
→ Future<
bool> -
Whether a refresh token exists in storage.
no setter
- dio → Dio
-
The Dio instance used for making HTTP requests.
final
- exceptionHandler → ExceptionHandler
-
Handler for processing exceptions.
final
- exceptionOptions → ExceptionOptions
-
Exception handling options.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
-
isAuthorized
→ Future<
bool> -
Whether the user is fully authorized (has header, JWT, and refresh token).
no setter
-
jwt
→ Future<
String?> -
Retrieves the stored JWT token, or
nullif not present.no setter - loggingOptions → LoggingOptions
-
Logging configuration options.
final
- options → RestApiClientOptions
-
General REST API client options (baseUrl, etc.).
final
-
refreshToken
→ Future<
String?> -
Retrieves the stored refresh token, or
nullif not present.no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- usesAuth → bool
-
Whether the Dio instance currently has an Authorization header set.
no setter
Methods
- Stores the JWT and refresh token, and sets the Authorization header.
-
clear(
) → Future - Clears all data from the auth storage.
-
executeTokenRefresh(
) → Future< void> - Executes the token refresh request to the configured endpoint.
-
init(
[bool migrateFromHive = true]) → Future - Initializes the auth handler and restores any previously stored tokens.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
refreshTokenCallback<
T> (RequestOptions requestOptions, [RequestInterceptorHandler? handler]) → Future< Response< T> ?> - Refreshes the token and retries the request with the new token.
-
toString(
) → String -
A string representation of this object.
inherited
-
unAuthorize(
) → Future< bool> - Clears all stored tokens and removes the Authorization header.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited