onRequest method

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

Intercepts outgoing requests to add the authorization headers.

Retrieves the access token from the tokenManager and uses the tokenRefreshStrategy to get the authorization headers. Adds these headers to the request options.

If an error occurs while retrieving the access token, rejects the request with a TokenManagerException.

Implementation

@override
Future onRequest(
    RequestOptions options, RequestInterceptorHandler handler) async {
  try {
    final accessToken = await tokenManager.getAccessToken();
    if (accessToken != null) {
      final headers =
          tokenRefreshStrategy.getAuthorizationHeaders(accessToken);
      options.headers.addAll(headers);
    }
    return handler.next(options);
  } catch (e) {
    return handler.reject(DioException(
      requestOptions: options,
      error: TokenManagerException(TokenErrorMessages.failedToGetAccessToken),
    ));
  }
}