onRequest method

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

Implementation

@override
void onRequest(
    RequestOptions options, RequestInterceptorHandler handler) async {
  logDebugMessage('SuperTokensInterceptorWrapper.onRequest: Intercepting request call');
  if (!SuperTokens.isInitCalled) {
    handler.reject(DioException(
      requestOptions: options,
      type: DioExceptionType.unknown,
      error: SuperTokensException(
          "SuperTokens.init must be called before using Client"),
    ));
    return;
  }

  if (!shouldRunDioInterceptor(options)) {
    logDebugMessage('SuperTokensInterceptorWrapper.onRequest: Skipping dio interceptor');
    return super.onRequest(options, handler);
  }

  logDebugMessage('SuperTokensInterceptorWrapper.onRequest: Running dio interceptor');
  if (Client.cookieStore == null) {
    logDebugMessage('SuperTokensInterceptorWrapper.onRequest: Initializing cookie store');
    Client.cookieStore = SuperTokensCookieStore();
  }

  logDebugMessage('SuperTokensInterceptorWrapper.onRequest: Removing auth header if it matches local token');
  options = await _removeAuthHeaderIfMatchesLocalToken(options);

  logDebugMessage('SuperTokensInterceptorWrapper.onRequest: Getting local session state');
  _preRequestLocalSessionState =
      await SuperTokensUtils.getLocalSessionState();
  String? antiCSRFToken = await AntiCSRF.getToken(
      _preRequestLocalSessionState.lastAccessTokenUpdate);

  if (antiCSRFToken != null) {
    logDebugMessage('SuperTokensInterceptorWrapper.onRequest: antiCSRFToken is not null');
    options.headers[antiCSRFHeaderKey] = antiCSRFToken;
  }

  SuperTokensTokenTransferMethod tokenTransferMethod =
      SuperTokens.config.tokenTransferMethod;
  options.headers["st-auth-mode"] = tokenTransferMethod.getValue();

  logDebugMessage('SuperTokensInterceptorWrapper.onRequest: Setting authorization header if required');
  options = await _setAuthorizationHeaderIfRequired(options);

  userSetCookie = options.headers[HttpHeaders.cookieHeader];

  String uriForCookieString = options.uri.toString();
  if (!uriForCookieString.endsWith("/")) {
    uriForCookieString += "/";
  }
  logDebugMessage('SuperTokensInterceptorWrapper.onRequest: uriForCookieString: ${uriForCookieString}');

  String? newCookiesToAdd = await Client.cookieStore
      ?.getCookieHeaderStringForRequest(Uri.parse(uriForCookieString));
  String? existingCookieHeader = options.headers[HttpHeaders.cookieHeader];

  // If the request already has a "cookie" header, combine it with persistent cookies
  if (existingCookieHeader != null) {
    logDebugMessage('SuperTokensInterceptorWrapper.onRequest: Combining cookie header values');
    options.headers[HttpHeaders.cookieHeader] =
        "$existingCookieHeader;${newCookiesToAdd ?? ""}";
  } else {
    logDebugMessage('SuperTokensInterceptorWrapper.onRequest: Adding new cookie header');
    options.headers[HttpHeaders.cookieHeader] = newCookiesToAdd ?? "";
  }

  logDebugMessage('SuperTokensInterceptorWrapper.onRequest: Injecting status check in validateStatus');
  var oldValidate = options.validateStatus;
  options.validateStatus = (status) {
    if (status != null &&
        status == SuperTokens.config.sessionExpiredStatusCode) {
      return true;
    }
    return oldValidate(status);
  };

  logDebugMessage('SuperTokensInterceptorWrapper.onRequest: Calling next on handler');
  handler.next(options);
}