If x number of requests are fired when the token is expired, then the other requests are not locked. Instead, x number requests get 401 error and the refreshAccessToken() is called x number of times.
But what I want to achieve is the refreshAccessToken() should be called only once and the other requests that are queued should be requested again with the new bearer token rather than failing with 401.
FYI: The BearerInterceptor adds the latest accessToken from the cache while creating a request.
Dio getAuthenticatedDio() {
authenticatedDio.interceptors.clear();
authenticatedDio.interceptors.add(BearerInterceptor(oauth));
authenticatedDio.interceptors
.add(InterceptorsWrapper(onRequest: (RequestOptions options) async {
return options; //continue
}, onResponse: (Response response) async {
print(response);
return response; // continue
}, onError: (DioError e) async {
print(e);
if (e.response.statusCode == 401) {
authenticatedDio.interceptors.requestLock.lock();
authenticatedDio.interceptors.responseLock.lock();
return oauth.refreshAccessToken().whenComplete(() {
authenticatedDio.interceptors.requestLock.unlock();
authenticatedDio.interceptors.responseLock.unlock();
return authenticatedDio.request(e.request.path, options: e.request);
});
}
return e;
}));
return authenticatedDio;}
Thanks in advance.
It seems that there are still issues encountered on multiple requests queued as mentioned here. There is no proper solutions yet as of this writing. I suggest to keep an eye on the GitHub issue for future updates.