How to handle when multiple async requests are being done
Hi guys. I'm have the following problem:
When my token is invalid it goes an requests a new token with the refresh with this code based on described in the docs:
// retrieve the JWT which is set during the Authentication
var jwt = $localStorage.JWT;
if(jwt)
{
// check if the token is expired?
if(jwtHelper.isTokenExpired(jwt))
{
// refresh the token
return $http({
url: 'http://mydomain.dev:8080/token',
method: 'GET',
headers: {Authorization: 'Bearer ' + jwt}
}).then( function successCallback(response)
{
// got a valid response so lets provide our storages with the new token
$localStorage.JWT = response.data
return response.data;
},
function errorCallback()
{
// somethings wrong with the token. Invalidate the tokens here
delete $localStorage.JWT;
});
}
else
{
return jwt;
}
But i have a route which resolves multiple things e.g. getAllUsers() getSomethingElseImportant()
so the interceptor will be called twice, and so the token will be acquired and blacklisted. But the second request will be doing the exact same thing and thus the backend will throw an exception that the token is blacklisted.
Any thoughts or solutions?
I ran into the same issue today. The trick is to save the promise of the token refresh http request somewhere and then use that promise across all subsequent requests also trying to get the refreshed token before the original call completes.
So your code would become something like:
// retrieve the JWT which is set during the Authentication
var jwt = $localStorage.JWT;
if(jwt)
{
// check if the token is expired?
if(jwtHelper.isTokenExpired(jwt))
{
// if we're not already refreshing the token, start a new request
if (!jwtHelper.refreshing) {
jwtHelper.refreshing = $http({
url: 'http://mydomain.dev:8080/token',
method: 'GET',
headers: {Authorization: 'Bearer ' + jwt}
}).then(function successCallback(response) {
// got a valid response so lets provide our storages with the new token
$localStorage.JWT = response.data
return response.data;
}, function errorCallback() {
// somethings wrong with the token. Invalidate the tokens here
delete $localStorage.JWT;
}).finally(function() {
// mark that we're done refreshing the token, so we can start a new request if needed
jwtHelper.refreshing = null;
})
}
return jwtHelper.refreshing;
}
else
{
return jwt;
}
@mikepostma that is very clever!
I am also facing the same kind of issue with JWT token usage. Below is the scenario of it. Please guide us if you know any approach or solution for this.
On every HTTP call, we are checking whether the token is present in local storage or not.
If it's not there in local storage or expired, we are keeping the current request on hold and will make a separate HTTP get a request to get token and store it in local storage. Once we get the token the previously hold request will be continued.
But if a page is having more than one HTTP request and the token is not present or expired, all those requests are kept on hold and making a call to get token. in this scenario, every request is getting a different token which is incorrect.
We need a way to overcome this issue.
What if you quickly open a few tabs at the same time? All opened tabs will at the same time realized there's a need for a new token, they will all request a new token, and they will all save their new token, but only the last tab to save the token will actually have the lastest token, hence blacklisting the other tab's recently acquired tokens.