Skip to content

Instantly share code, notes, and snippets.

@tyb
Created December 7, 2020 21:01
Show Gist options
  • Save tyb/00cb42e46d27e583894eb5df7d83d048 to your computer and use it in GitHub Desktop.
Save tyb/00cb42e46d27e583894eb5df7d83d048 to your computer and use it in GitHub Desktop.
Created with Copy to Gist
You could check the url of the request to prevent an infinite loop. Also instead of returning the config directly you could return a promise that resolves with it so you can wait until you have a valid token. e.g.
{
request: function(config) {
if(config.url != 'my/refresh/url') {
var promiseToHaveValidToken;
var expiry = new Date($window.sessionStorage['userInfo-tokenexpiry']);
var now = new Date();
if (now > expiry ) {
promiseToHaveValidToken = $http.get('my/refresh/url').then(function (response) {
return response.data.token;
});
} else {
promiseToHaveValidToken = $q.resolve(sessionStorage['userInfo-accesstoken']);
}
return promiseToHaveValidToken.then(function (token) {
config.headers['Authorization'] = 'Bearer ' + token;
return config;
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment