如何限制登录用户在angular js中的登录页面或注册页面上移动

how to restrict logged user to move on login page or signup page in angular js?

本文关键字:登录 移动 注册 用户 何限制 angular js      更新时间:2023-09-26

我有以下代码:

.run(function($rootScope, $location, Auth) {
    //Redirect to login if route requires auth and you're not logged in
    $rootScope.$on('$routeChangeStart', function(event, next) {
       Auth.isLoggedInAsync(function(loggedIn) {
        if (!loggedIn) {//when user is not logged in
          if ($location.path() == "/participant/signup" || $location.path() == "/researcher/signup" || $location.path() == "/participant/login" || $location.path() == "/researcher/login" || $location.path() == "/admin/login");
          else{
            $location.path('/homePage');
          }
        }
        else if(loggedIn){ //when user loged in
          if ($location.path() == "/participant/signup" || $location.path() == "/researcher/signup" || $location.path() == "/participant/login" || $location.path() == "/researcher/login" || $location.path() == "/admin/login"){
            event.preventDefault();
            //event.stopPropagation();
          }
        }
      });
    });
  })

我想限制登录用户在登录或注册页面上移动。上面的代码不起作用,请告诉我哪里做错了。如果你有一个很好的方法来处理这个问题,请建议?

Define an interceptor to handle user status
angular.module('App').factory('authInterceptor', ['$location', 'Auth', function ($location, Auth) {
        var authInterceptorService = {};
        if (!Auth.isLoggedIn)
            $location.url('/login');
        return authInterceptorService;
    }]);

在App.js 中

angular.module('App').config(function ($httpProvider) {
    $httpProvider.interceptors.push('authInterceptorService');
});