[英]AngularJs - Skip login page if already logged in
I have implemented an Authentication system for my app. 我已经为我的应用程序实现了身份验证系统。 I use
$routeChangeStart
(in run) to check if the user is logged in and redirect him to login if not. 我使用
$routeChangeStart
(运行中)检查用户是否已登录,如果没有登录,则将其重定向到登录名。 This works fine. 这很好。 But, I can access the login page via the url, I want to send the user to the dashboard if he's already logged in. What is the proper way to do this?
但是,我可以通过url访问登录页面,如果用户已经登录,我想将其发送到仪表板。执行此操作的正确方法是什么?
Guys try this elegant solution: 伙计们尝试这个优雅的解决方案:
.run(function ($rootScope, $location, authService) {
$rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams) {
if (to.requireAuth && !authService.isAuthed()) {
$location.path("/login");
}
else if (to.name == 'login' && authService.isAuthed()) {
ev.preventDefault();
$location.path("/dashboard");
}
});
})
Try 尝试
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl) {});
Details here https://docs.angularjs.org/api/ng/service/$location 详情在这里https://docs.angularjs.org/api/ng/service/$location
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.