Last active
January 4, 2016 03:29
-
-
Save ndamnjanovic/8561702 to your computer and use it in GitHub Desktop.
angular authentication and routing
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
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