Skip to content

Instantly share code, notes, and snippets.

@alex-ross
Last active December 24, 2015 05:39
Show Gist options
  • Select an option

  • Save alex-ross/6751583 to your computer and use it in GitHub Desktop.

Select an option

Save alex-ross/6751583 to your computer and use it in GitHub Desktop.
/**
* Redirect unauthorized users to given path
* whenever any response returns http code 401.
*/
angular.module('RedirectUnauthorized')
.config(['$httpProvider', function($httpProvider) {
// Where should we redirect unauthorized users?
var loginPath = '/signin';
var interceptor = ['$q', function ($q) {
function success(response) {
return response;
}
function error(response) {
if (response.status == 401) {
location.href = loginPath;
}
return $q.reject(response);
}
return function (promise) {
return promise.then(success, error);
}
}];
$httpProvider.responseInterceptors.push(interceptor);
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment