I'm stuck on this problem and could use some guidance. My goal is to refresh a user's access token when it is close to expiration.
The authService.isUserLoggedIn()
function returns a promise that checks if the user is logged in. If not, the user's access token needs to be refreshed.
The issue I'm facing is that authService.isUserLoggedIn()
is an asynchronous call, so the interceptor finishes its execution before the promise is resolved, leading to the Authorization header not being updated with the new token.
I have been trying to find a way to wait for the promise to resolve before continuing with the script, but so far, I haven't been successful in achieving the desired outcome.
Below is the current code snippet:
.factory('SEHttpInterceptor', function($injector, ngWebApiSettings) {
return {
// optional method
'request': function(config) {
// add Authorization header if available
if (config.url.indexOf(ngWebApiSettings.apiServiceBaseUri) >-1){
var authService = $injector.get('authService2');
authService.isUserLoggedIn().then(function(response){
var authData = $injector.get('$localStorage').getObject("authorizationData");
config.headers.Authorization = 'Bearer ' + authData.token;
});
}
return config;
}
};
});