executeTokenRefresh method

Future<void> executeTokenRefresh()

Refreshes the token by calling specified refresh-token endpoint

Implementation

Future<void> executeTokenRefresh() async {
  final newDioClient = Dio(BaseOptions()
    ..baseUrl = options.baseUrl
    ..contentType = Headers.jsonContentType);

  if (loggingOptions.logNetworkTraffic) {
    newDioClient.interceptors.add(
      PrettyDioLogger(
        responseBody: loggingOptions.responseBody,
        requestBody: loggingOptions.requestBody,
        requestHeader: loggingOptions.requestHeader,
        request: loggingOptions.request,
        responseHeader: loggingOptions.responseHeader,
        compact: loggingOptions.compact,
        error: loggingOptions.error,
      ),
    );
  }

  if (options.overrideBadCertificate && !kIsWeb) {
    (dio.httpClientAdapter as IOHttpClientAdapter).createHttpClient = () {
      final client = HttpClient();

      client.badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;

      return client;
    };
  }

  final currentJwt = await jwt;
  final currentRefreshToken = await refreshToken;

  final response = await newDioClient.post(
    authOptions.refreshTokenEndpoint,
    options: Options(
      headers: authOptions.refreshTokenHeadersBuilder
              ?.call(currentJwt ?? '', currentRefreshToken ?? '') ??
          {
            RestApiClientKeys.authorization: 'Bearer $currentJwt',
          },
    ),
    data: authOptions.refreshTokenBodyBuilder
            ?.call(currentJwt ?? '', currentRefreshToken ?? '') ??
        {
          authOptions.refreshTokenParameterName: currentRefreshToken,
        },
  );

  final resolvedJwt = authOptions.resolveJwt!(response);
  final resolvedRefreshToken = authOptions.resolveRefreshToken!(response);

  await authorize(jwt: resolvedJwt, refreshToken: resolvedRefreshToken);
}