Skip to content

Instantly share code, notes, and snippets.

@ndamnjanovic
Last active January 4, 2016 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndamnjanovic/8561702 to your computer and use it in GitHub Desktop.
Save ndamnjanovic/8561702 to your computer and use it in GitHub Desktop.
angular authentication and routing
In case you want to do authentication and redirection, depending on whether user is logged in or not,
you can do it by listening $routeChangeStart event.
This way, you don't need to use additional plugins,
or to add 'resolve' parameters to your routes (which can be difficult to maintain).
In following example, if non-logged user tries to reach authenticated route, user will be redirected to login page.
If logged user tries to reach login/register page, user will be redirected to home page.
$rootScope.$on('$routeChangeStart', function(event, next, current){
if(AuthFactory.isAuthenticated()){
if(AuthFactory.isAuthRoute(next.originalPath)){
$route.reload();
$location.path('/');
}
} else {
if(!AuthFactory.isAuthRoute(next.originalPath)){
$route.reload();
$location.path('/login');
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment