Created
December 7, 2020 21:01
-
-
Save tyb/00cb42e46d27e583894eb5df7d83d048 to your computer and use it in GitHub Desktop.
Created with Copy to Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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