onRequest method

  1. @override
void onRequest(
  1. RequestOptions options,
  2. RequestInterceptorHandler handler
)

Called when the request is about to be sent.

Implementation

@override
void onRequest(
  RequestOptions options,
  RequestInterceptorHandler handler,
) async {
  if (_isPublic(options.path)) return handler.next(options);

  if (_tokenManager.isExpired) {
    final ok = await _coordinator.ensureRefreshed();
    if (!ok) {
      return handler.reject(DioException(
        requestOptions: options,
        error: const UnauthorizedException(),
      ));
    }
  }

  options.headers['Authorization'] = 'Bearer ${_tokenManager.accessToken}';
  handler.next(options);
}