简体   繁体   English

AngularJs-如果已经登录,则跳过登录页面

[英]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");
            }
        });
    })
  • if the user is not logged in and login is required it redirect to login. 如果用户未登录并且需要登录,它将重定向到登录。
  • if the user try to get to login and he already logged in, prevent him from doing it. 如果用户尝试登录并已经登录,请阻止他登录。
  • mark every path as "requireAuth" and it will work. 将每个路径标记为“ requireAuth”,它将起作用。
  • place your code in app.js 将您的代码放在app.js中

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.

相关问题 Ionic 1 - 如果用户未登录则显示登录页面,如果用户已登录则跳过 - Ionic 1 - Show login page if user not logged and skip if user is already logged 如果用户已登录离子,如何跳过登录页面 - how to skip login page if user already logged in in ionic 如果用户已登录离子框架,如何跳过登录页面 - How to skip login page if user is already logged in ionic framework 如果已经使用angularjs登录,如何避免显示登录页面? - How to avoid showing login page if already logged on with angularjs? Angularjs处理登录和登录页面 - Angularjs handle login and logged in page 限制用户登录后重定向到登录页面,即使他按角度按浏览器的后退按钮? - restrict the redirection to login page once the user has already logged in even if he press the back button of browser in angular? 当用户未登录或用户尝试使用angularjs直接从URL访问页面时,重定向到登录 - Redirect to login when user is not logged in or user tries to access page directly from URL using angularjs 如果用户已登录,如何限制后退按钮,或直接在angularjs中访问登录页面 - if user is logged in, how to restrict the back button, or accessing the login page directly in angularjs angularjs + ui-router:当用户未登录每个状态更改时,重定向到登录页面 - angularjs + ui-router: redirect to login page when user is not logged in on each state change 如果未登录,angularjs重新登录到登录页面 - angularjs redircet to login page if not login
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM