I am struggling a little bit with handling 401 errors application wide.
Some other posts where not really helping me: Angularjs - handling 401's for entire app 401 unauthorized error handling in AngularJS
I have to send an authentication token, if it is defined on every request, thats why I need a request part in the Interceptor.
My current approach looks like:
module.factory('authInterceptor', function ($rootScope, $q, $window, $location) {
return {
request: function (config) {
config.headers = config.headers || {};
if ($window.sessionStorage.token) {
config.headers.Authorization = 'Bearer ' + $window.sessionStorage.token;
}
else{
console.log("authInterceptor: No Token to send")
}
return config;
},
response: function (response) {
if (response.status == 401) {
console.log("No Logged In");
$location.path('/login');
}
return response || $q.when(response);
},
responseError: function(rejection){
var defer = $q.defer();
if(rejection.status == 401){
console.log("401 Rejection");
console.dir(rejection);
}
defer.reject(rejection);
return defer.promise;
}
};
});
As far as my debugging goes if a 401 happens after request, it will be handled in the repsonse function. Which itself gives way a promise to the original caller.
If i comment out $q.when(response) and only return the repsonse there is no change.
Response is called even before the original caller gets .error
Thx for any ideas. Hopefully I am not to far away from a solution.