refreshTokenCallback<T> method

Future<Response<T>?> refreshTokenCallback<T>(
  1. DioException e
)

Gets called when response status code is UnAuthorized and refreshes the token by calling specified refresh-token endpoint

Implementation

Future<Response<T>?> refreshTokenCallback<T>(DioException e) async {
  if (authOptions.resolveJwt != null &&
      authOptions.resolveRefreshToken != null) {
    await executeTokenRefresh();

    final currentJwt = await jwt;

    final requestOptions = e.requestOptions;

    //Set for current request
    if (requestOptions.headers.containsKey(RestApiClientKeys.authorization)) {
      requestOptions.headers.update(
          RestApiClientKeys.authorization, (v) => 'Bearer $currentJwt');
    } else {
      requestOptions.headers
          .addAll({RestApiClientKeys.authorization: 'Bearer $currentJwt'});
    }

    exceptionOptions.reset();

    return await dio.request(
      requestOptions.path,
      cancelToken: requestOptions.cancelToken,
      data: requestOptions.data,
      onReceiveProgress: requestOptions.onReceiveProgress,
      onSendProgress: requestOptions.onSendProgress,
      queryParameters: requestOptions.queryParameters,
      options: Options(
        method: requestOptions.method,
        sendTimeout: requestOptions.sendTimeout,
        receiveTimeout: requestOptions.receiveTimeout,
        extra: requestOptions.extra,
        headers: requestOptions.headers,
        responseType: requestOptions.responseType,
        contentType: requestOptions.contentType,
        validateStatus: requestOptions.validateStatus,
        receiveDataWhenStatusError: requestOptions.receiveDataWhenStatusError,
        followRedirects: requestOptions.followRedirects,
        maxRedirects: requestOptions.maxRedirects,
        requestEncoder: requestOptions.requestEncoder,
        responseDecoder: requestOptions.responseDecoder,
        listFormat: requestOptions.listFormat,
      ),
    );
  }

  return null;
}