Skip to content

Instantly share code, notes, and snippets.

@alchemycs
Created August 31, 2013 03:21
Show Gist options
  • Save alchemycs/6396034 to your computer and use it in GitHub Desktop.
Save alchemycs/6396034 to your computer and use it in GitHub Desktop.
app.config(function($httpProvider) {
$httpProvider.interceptors.push(function($rootScope, $location, $q) {
return {
'request': function(request) {
// if we're not logged-in to the AngularJS app, redirect to login page
$rootScope.loggedIn = $rootScope.loggedIn || $rootScope.username;
if (!$rootScope.loggedIn && $location.path() != '/login') {
$location.path('/login');
}
return request;
},
'responseError': function(rejection) {
// if we're not logged-in to the web service, redirect to login page
if (rejection.status === 401 && $location.path() != '/login') {
$rootScope.loggedIn = false;
$location.path('/login');
}
return $q.reject(rejection);
}
};
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment