2014-09-19 151 views
1

我有以下代码:如何限制登录用户在登录页面或注册页面上以角度js移动?

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

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

+0

使用httpInterceptor – 2014-09-19 08:26:00

回答

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