angularjs - How to protect a route from being accessed when logged in? -
i have error handling function responds errors in routing, , redirects user landing page if user not authenticated:
core.run(['$rootscope', '$location', function($rootscope, $location) { $rootscope.$on('$routechangeerror', function(event, next, previous, error) { if (error === 'auth_required') { $location.path('/'); } }); }]);
now want reverse this, if user authenticated, route not available:
.when('/', { templateurl: 'views/pages/landing.html', resolve: { 'isauth': ['fbrefs', function(fbrefs) { return fbrefs.getauthobj().$waitforauth(); }] } })
how alter resolve
above check if user is authenticated , if so, redirect /home
instead of /
?
i tried manually rejecting promise returned resolve makes route disabled no matter if user authenticated or not.
you handle manually $q
.when('/', { templateurl: 'views/pages/landing.html', resolve: { 'isauth': ['fbrefs', function(fbrefs, $q) { var onloggedin = function() { return $q.reject('no_auth_required'); } var onloggedout = function(fbdata) { return fbdata; } return fbrefs.getauthobj().$requireauth() .then(onloggedin, onloggedout); }] } })
...
core.run(['$rootscope', '$location', function($rootscope, $location) { $rootscope.$on('$routechangeerror', function(event, next, previous, error) { if (error === 'auth_required') { $location.path('/'); } else if (error === 'no_auth_required') { $location.path('/home'); } }); }]);
Comments
Post a Comment