AuthInterceptor class
Authentication interceptor with automatic token refresh
Features:
- Automatic token injection
- Token refresh on 401 errors
- Request queuing during refresh
- Race condition prevention
- Configurable token format
Example:
final authInterceptor = AuthInterceptor(
tokenManager: myTokenManager,
dioProvider: KeystoneNetwork.dioProvider,
);
KeystoneNetwork.initialize(
baseUrl: 'https://api.example.com',
interceptors: [authInterceptor],
);
// Now all requests automatically include auth token
// and will refresh on 401 errors
- Inheritance
-
- Object
- Interceptor
- AuthInterceptor
Constructors
- AuthInterceptor({required TokenManager tokenManager, required DioProvider dioProvider, String authHeaderKey = 'Authorization', String tokenFormatter(String token) = _defaultTokenFormatter, bool shouldRefreshToken(int? statusCode)?})
Properties
- authHeaderKey → String
-
Header key for authorization (default: 'Authorization')
final
- dioProvider → DioProvider
-
DioProvider for making retry requests
Use KeystoneNetwork.dioProvider or create your own
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- shouldRefreshToken → bool Function(int? statusCode)?
-
Function to determine if token should be refreshed based on status code
Default: refresh on 401 status code
final
- tokenFormatter → String Function(String token)
-
Function to format the token (default: 'Bearer {token}')
final
- tokenManager → TokenManager
-
Token manager for accessing and refreshing tokens
final
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onError(
DioException err, ErrorInterceptorHandler handler) → void -
Called when an exception was occurred during the request.
override
-
onRequest(
RequestOptions options, RequestInterceptorHandler handler) → void -
Called when the request is about to be sent.
override
-
onResponse(
Response response, ResponseInterceptorHandler handler) → void -
Called when the response is about to be resolved.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited