Skip to content

Instantly share code, notes, and snippets.

@oaleynik
Created January 2, 2014 12:32
Show Gist options
  • Save oaleynik/8218568 to your computer and use it in GitHub Desktop.
Save oaleynik/8218568 to your computer and use it in GitHub Desktop.
angular.module('app', [])
.config(function($routeProvider) {
$routeProvider.when('/needsauth', {
// config for controller and view
resolve: {
'auth': function(AuthService) {
return AuthService.authenticate();
}
}
});
})
.factory('AuthService', function($q) {
function _authenticate() {
var isAuthenticated;
// authentication logic
if(isAuthenticated) {
return true;
} else {
return $q.reject('Not Authenticated');
}
};
})
.run(function($rootScope, $location) {
$rootScope.$on('$routeChangeError', function(current, previous, rejection) {
if(rejection === 'Not Authenticated') {
$location.path('/login');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment