2014-09-19 152 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'); 
});