onRequest method

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

Called before a request is sent.

Implementation

@override
void onRequest(RequestOptions options, handler) {
  if (isPreemptivelyRefreshBeforeExpiry &&
      !authOptions.ignoreAuthForPaths.contains(options.path)) {
    try {
      final bearer = options.headers[RestApiClientKeys.authorization];
      final jwt = bearer != null
          ? (bearer as String).replaceAll('Bearer ', '')
          : '';

      if (jwt.isEmpty) {
        handler.next(options);
      } else {
        final isExpired = JwtDecoder.isExpired(jwt);

        if (isExpired) {
          authHandler.refreshTokenCallback(options, handler);
        } else {
          handler.next(options);
        }
      }
    } catch (e) {
      debugPrint(
        'Rest API client - Refresh token interceptor - exception: $e',
      );

      handler.next(options);
    }
  } else {
    handler.next(options);
  }
}