Skip to content

Instantly share code, notes, and snippets.

@djds4rce
Last active December 21, 2015 00:49
Show Gist options
  • Save djds4rce/6223109 to your computer and use it in GitHub Desktop.
Save djds4rce/6223109 to your computer and use it in GitHub Desktop.
Http Response interceptor. Handles 401 requests from the server
//Works for 1.1.x versions. 1.0.x is similar and can be figured out using code comments
myapp.factory('myHttpResponseInterceptor',['$q','$location',function($q,$location){
return {
response: function(response){
return promise.then(
function success(response) {
return response;
},
function error(response) {
if(response.status === 401){
$location.path('/signin');
return $q.reject(response);
}
else{
return $q.reject(response);
}
});
}
}
}]);
//Http Intercpetor to check auth failures for xhr requests
myapp.config(['$httpProvider',function($httpProvider) {
$httpProvider.interceptors.push('myHttpResponseInterceptor');
}]);
@checketts
Copy link

On line 5 promise isn't defined. Should that be some $q value?

@spoatacus
Copy link

Line 4 should be:

    response: function(promise){

http://docs.angularjs.org/api/ng.$http (Response Interceptor section)

@DennisDe
Copy link

Hey there! Right now as I try to use the above snippet I always get the following JavaScript error

TypeError: Object # has no method 'then'

shown in the Chrome dev-tools console. I already included the fix mentioned above to my snippet implementation. Shouldn't it work out of the box after having adapted the "myapp" to fit to my project?

Great snippet though and thx a lot for sharing so far!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment