onRequest method

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

Called when the request is about to be sent.

Implementation

@override
void onRequest(
  RequestOptions options,
  RequestInterceptorHandler handler,
) async {
  // Skip auth header for certain endpoints
  // Usage: dio.get('/public', options: Options(extra: {'skipAuth': true}))
  if (options.extra['skipAuth'] == true) {
    return handler.next(options);
  }

  // Get and add token
  final token = await tokenManager.getAccessToken();
  if (token != null && token.isNotEmpty) {
    options.headers[authHeaderKey] = tokenFormatter(token);
  }

  handler.next(options);
}