Last active
December 21, 2015 00:49
-
-
Save djds4rce/6223109 to your computer and use it in GitHub Desktop.
Http Response interceptor. Handles 401 requests from the server
This file contains hidden or 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
//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'); | |
}]); |
Line 4 should be:
response: function(promise){
http://docs.angularjs.org/api/ng.$http (Response Interceptor section)
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
On line 5 promise isn't defined. Should that be some $q value?