Pulling my hair on it. I want to update the access token if the user access token expires.
authService.isUserLoggedIn() returns a promise and checks if the user is logged in or not. If the user access token is not updated.
However, the problem is that authService.isUserLoggedIn() is an asynchronous call and before it returns the value, the interceptor will complete its task and the authorization header will not be filled with a new token ...
I was looking for a way to wait for the promise to resolve before the script continues. Unfortunately, I cannot complete what is required.
code:
.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; } }; });
source share