Ned*_*Ned 6 authentication angularjs url-redirection angularjs-routing angularjs-authentication
我正在尝试创建基本验证,无论用户是否可以访问某些路由.我在这方面取得了进展,但有一点我无法弄清楚.
我正在使用$ locationChangeStart来监控路由更改.场景是:1.如果用户已登录,则允许他访问除auth路由(登录,注册)之外的所有路由.我通过从我的AuthFactory 2调用方法isAuthenticated()来检查这个.如果用户没有登录,那么他只访问登录和注册路由.应该防止任何其他路由,并且在这种情况下应该将用户重定向到登录.
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
if(AuthFactory.isAuthenticated()){
if(AuthFactory.isAuthRoute(newUrl)){
event.preventDefault();
$location.path('/');
}
} else {
if(!AuthFactory.isAuthRoute(newUrl)){
event.preventDefault();
$location.path('/login');
}
}
});
Run Code Online (Sandbox Code Playgroud)
让我烦恼的是那个带有preventDefault()的人.如果应用程序到达带有preventDefault()的代码,location.path()那么之后就会发生这种情况.
但是,如果我删除event.preventDefault(),location.path()工作.问题是,我需要阻止,以防非记录尝试访问某些非auth页面.
基本上,我希望能够根据请求的路线阻止或重定向.这样做的正确方法是什么?
| 归档时间: |
|
| 查看次数: |
32193 次 |
| 最近记录: |