onRequest method

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

Called when the request is about to be sent.

Implementation

@override
Future<void> onRequest(
  RequestOptions options,
  RequestInterceptorHandler handler,
) async {
  // Check if current path is in the exclude list.
  final bool isExcluded = excludePaths?.any((path) => options.path.contains(path)) ?? false;

  if (!isExcluded) {
    final token = await tokenProvider();
    if (token != null && token.isNotEmpty) {
      options.headers[headerKey] = '$authPrefix $token'.trim();
    }
  }

  return super.onRequest(options, handler);
}