TokenKeeperInterceptor class

Dio interceptor that authenticates requests with TokenKeeper.

Behaviour:

  1. Before each request, fetches a valid token via TokenKeeper.getValidToken and sets the auth header. If no token is available the request proceeds without the header so anonymous endpoints still work.
  2. On a 401 (configurable via shouldRefreshOn), runs a single-flight refresh via TokenKeeper.forceRefresh and retries the request exactly once. The retry is tagged on RequestOptions.extra so it can never recurse.
  3. If the refresh fails the original error is forwarded unchanged. The TokenKeeper itself emits TokenClearedEvent for unauthorized refresh failures, which your app should listen to in order to log the user out.

Constructors

TokenKeeperInterceptor({required TokenKeeper keeper, required Dio dio, String headerName = 'Authorization', String scheme = 'Bearer', bool shouldRefreshOn(Response? response)?, void onRefreshFailed(Failure failure)?})
Creates an interceptor.

Properties

dio → Dio
Client used to retry requests after a successful refresh.
final
hashCode int
The hash code for this object.
no setterinherited
headerName String
Header name to set. Defaults to Authorization.
final
keeper TokenKeeper
Token source.
final
onRefreshFailed → void Function(Failure failure)?
Called when a refresh triggered by a 401 fails. Receives resilify's non-generic Failure.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
scheme String
Scheme prefix for the header. Defaults to Bearer.
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onError(DioException err, ErrorInterceptorHandler handler) Future<void>
Called when an exception was occurred during the request.
onRequest(RequestOptions options, RequestInterceptorHandler handler) Future<void>
Called when the request is about to be sent.
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