Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to hold another api untill where current api getting refresh token #1848

Closed
Devandhiran637 opened this issue Jun 2, 2023 · 3 comments
Closed
Labels
i: wontfix This will not be worked on

Comments

@Devandhiran637
Copy link

Devandhiran637 commented Jun 2, 2023

Request Statement

In dio Interceptor and the user touch multiple events in a whole app and the api are triggered multiple times and every request call refresh token at the same time and return 400 issue logout to login page

I also tried Future.wait but it trigerred multiple api are given at same time, it will wait return respone

but here the user touch other event where the current api aldready loading and event call api then two api getting refresh will return issue session expired

So how to hold another api until where current api get refresh token then allow that api get data

`class Interceptor{
static Dio dio({dynamic data}) {
final dio = Dio();

dio.interceptors.add(AuthInterceptor(dio: dio));

return dio;

}
}`

`class AuthInterceptor extends QueuedInterceptor {
final Dio? dio;
BuildContext? context;

AuthInterceptor({this.dio, this.context});

@OverRide
void onRequest(
RequestOptions options, RequestInterceptorHandler handler) async {
final accessToken = await Mystorage.read('access');
final refreshToken = await Mystorage.read('refresh');

if (accessToken == null || refreshToken == null) {
  Nav.navKey.currentState!
      .pushReplacementNamed('/login');
}



bool accessisExpired = JwtDecoder.isExpired(accessToken ?? " ");


if (accessisExpired ) {


  try {
    _refresh = await Api.refresh();
  } on DioError catch (e) {
    if (e.type == DioErrorType.sendTimeout ||
        e.type == DioErrorType.connectionTimeout ||
        e.type == DioErrorType.receiveTimeout) {
      return handler.next(options);
    } else if (e.error is SocketException) {
      print(e);
      print('f net');

      return handler.next(options);
    }
  }
  if (_refresh == true) {
    
    options.headers["Authorization"] =
        "Bearer ${await Mystorage.read("access")}";
   
    options.headers["accept"] = "application/json";
    options.connectTimeout = const Duration(seconds: 120);
    options.receiveTimeout = const Duration(seconds: 90);

    return handler.next(options);
  } else if (_refresh == null) {
   
   Nav.navKey.currentState!
        .pushReplacementNamed('/login');
  } else {
   
    bool anotherchech = JwtDecoder.isExpired(
        await Mystorage.read('access') ?? " ");
    if (anotherchech) {
      
      Nav.navKey.currentState!
          .pushReplacementNamed('/loginpage');
      
    } else {
      print('f 6');
      options.headers["Authorization"] =
          "Bearer ${await Mystorage.read("access")}";
      options.headers["x-api-key"] = Url.clientId;
      options.headers["accept"] = "application/json";

      options.connectTimeout = const Duration(seconds: 120);
      options.receiveTimeout = const Duration(seconds: 90);

      return handler.next(options);
    }
  }
} else {
  print('4');
  options.headers["Authorization"] =
      "Bearer ${await Mystorage.read("access")}";
  options.headers["x-api-key"] = Url.clientId;
  options.headers["accept"] = "application/json";

  options.connectTimeout = const Duration(seconds: 120);
  options.receiveTimeout = const Duration(seconds: 90);

  return handler.next(options);
}

}

@OverRide
void onError(DioError err, ErrorInterceptorHandler handler) async {
}
}
class Repo{
Future callapi(){
var response = await Interceptor.dio(data: typeId).post(
"${Api.url}/xxxxxx",
data: l,
);

return response;

}
}`

Solution Brainstorm

No response

@Devandhiran637 Devandhiran637 added the s: feature This issue indicates a feature request label Jun 2, 2023
@Devandhiran637 Devandhiran637 changed the title how to hold another api untill where current api getting refresh token flutter how to hold another api untill where current api getting refresh token Jun 2, 2023
@stngcoding
Copy link

did you find any solutions, can you share it here ?

@AlexV525 AlexV525 added i: wontfix This will not be worked on and removed s: feature This issue indicates a feature request labels Jul 16, 2023
@Devandhiran637
Copy link
Author

Devandhiran637 commented Jul 17, 2023

I'm not sure, try adding queue package
`
@OverRide
void onRequest(
RequestOptions options, RequestInterceptorHandler handler) async {
final accessToken = await Secure.read('accesstoken');
final refreshToken = await Secure.read('refreshtoken');

var _refreshed = false;

bool accesscheck = JwtDecoder.isExpired(accessToken ?? " ");



if (accessTokenHasExpired) {

  final queue = Queue();
  await queue.add(() async {
    try {
      _refreshed = await Authenticateapi.refreshtoken();
    } on DioError catch (e) {
      if (e.type == DioErrorType.sendTimeout ||
          e.type == DioErrorType.connectionTimeout ||
          e.type == DioErrorType.receiveTimeout) {
   
        _refreshed = true;
        return handler.next(options);
      } else if (e.error is SocketException ||
          e.type == DioErrorType.unknown) {
      
        _refreshed = true;
        return handler.next(options);
      } else {
        _refreshed = true;
        return handler.next(options);
      }
    }
  });`

@duytq94
Copy link

duytq94 commented Feb 8, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
i: wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

4 participants