如何限制登录用户在角度js的登录页面或注册页面上移动?

时间:2014-09-19 08:24:49

标签: javascript angularjs

我有以下代码:

.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();
          }
        }
      });
    });
  })

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

1 个答案:

答案 0 :(得分:1)

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');
});