Home > Software design >  How to handle multiple API requests when the auth token is expired?
How to handle multiple API requests when the auth token is expired?

Time:05-13

I have a use case wherein I am calling 2 different APIs at almost the same time, but while executing the 1st API it turns out that my auth token is expired. Now I can pause the execution of API 1 while I get a new token, I want the execution of the 2nd API to pause too, and on getting the valid token, it should resume.

CodePudding user response:

You can do this with dio. You can lock particular Object by lock(). If lock() is called all the further request will be placed in a queue, the calls will be made after when the unlock() is called.

Dio _dio= Dio();

_dio.lock();

_dio.unlock();

CodePudding user response:

Usually this is managed by synchronizing token refresh, as in these code snippets of mine:

The technique is to let multiple API calls fail, then refresh the token once, then retry all API calls. This plumbing is then seamless to the rest of the app's code.

  • Related