2017-01-23 139 views
0

我希望Angular在每個路由更改時檢查身份驗證。我的代碼看起來像這樣。路由更改的身份驗證

config.js

angular.module('message').run(['$rootScope', '$location', 'Auth', function ($rootScope, $location, Auth) { 
    $rootScope.$on('$routeChangeStart', function (event) { 

     if (!Auth.isLoggedIn()) { 
      console.log('DENY'); 
      event.preventDefault(); 
      $location.path('/Login'); 
     } 
     else { 
      console.log('ALLOW'); 
      $location.path('/home'); 
     } 
    }); 
}]); 

authenticationService.js

var app=angular.module('message'); 
app.factory('Auth', function($rootScope){ 


return{ 
    setUser : function(aUser){ 
     $rootScope.user = aUser; 
    }, 
    isLoggedIn : function(){ 
     return($rootScope.user)? $rootScope.user : false; 
    } 
    } 
}) 

我app.js看起來像我面臨這個

var app=angular.module('message',['restangular','ngRoute']); 


app.config(['$routeProvider', function ($routeProvider){ 
    $routeProvider 
    .when("/", { 
     templateUrl : "view/user.html", 
     controller : "loginController" 
    }) 
    .when("/NewUser", { 
     templateUrl : "view/addUser.html", 
     controller : "MainCtrl" 
    }) 
    .when("/ViewUsers", { 
     templateUrl : "view/ViewUsers.html", 
     controller : "MainCtrl" 
    }) 
    .when("/EditUser/:id", { 
     templateUrl : "view/EditUser.html", 
     controller : "MainCtrl" 
    }) 
    .when("/Register", { 
     templateUrl : "view/Register.html", 
     controller : "RegisterController" 
    }) 
    .when("/Login", { 
     templateUrl : "view/user.html", 
     controller : "loginController" 
    }) 
    .when("/Home", { 
     templateUrl : "view/Home.html", 
     controller : "HomeController" 
    }) 
    .when("/ForgotPassword", { 
     templateUrl : "view/Home.html", 
     controller : "ForgotController" 
    }); 

}]); 

app.controller('MainCtrl',function($scope,Restangular,$location,$routeParams,$http,Auth){ 
    Restangular.setBaseUrl('webapi'); 

    //watch to work on user state changed 
    $scope.$watch(Auth.isLoggedIn, function (value, oldValue) { 

      if(!value && oldValue) { 
       console.log("Disconnect"); 
       $location.path('/Login'); 
      } 

      if(value) { 
       console.log("Connect"); 
       //Do something when the user is connected 
       $location.path('/Home'); 
      } 

      }, true); 


    //setting profile name as id for profiles model 

    var index = $routeParams.id; 
    $scope.index=index; 
    console.log($scope.index) 
    var profiles = Restangular.all('profiles'); 

    Restangular.setRestangularFields({ 
      id: "_id" 
     }); 

    Restangular.extendModel('profile', function(profile) { 
      profile.id = profile.profileName; 
     }); 

    Restangular.addResponseInterceptor(function (data, operation, what, url, response, deferred) { 
     if (operation == "getList") { 
      return response.data; 
     } 
     return response+=response.data; 
    }); 

    data=function(user){ 
     consle.log(user); 
    } 


    // This will query /profiles and return a promise. 
    profiles.getList().then(function(profiles) { 
     $scope.profiles = profiles;  
     $scope.saveUser=function(){ 
      $scope.profile.profileName=$scope.profile.firstName+$scope.profile.lastName; 
      console.log($scope.profile); 
      profiles.post($scope.profile); 
    } 

     $scope.showUser=function(id){ 
      $location.path('/EditUser/'+id); 
     } 
     $scope.editUser=function(id){ 
      //put requestusing http 
      var probject={}; 
      probject.firstName=profiles[id].firstName; 
      probject.lastName=profiles[id].lastName; 
      probject.profileName=profiles[id].profileName; 
      probject.id=profiles[id].id; 
      probject.created=profiles[id].created; 
      $http.put("http://localhost:8080/messenger/webapi/profiles/"+$scope.profiles[id].profileName,probject).then(function(data){ 
      console.log(data); 
      }); 


     } 
    }); 




     $scope.go = function (path) { 
      $location.path(path); 
     }; 
}); 

問題是沒有配置我的代碼運行正常.js 但每當我使用身份驗證服務註冊$ onroutechange事件config.js我的代碼停止工作,沒有提供任何錯誤。

我可能會做一些愚蠢的事情,因爲我是新角色。 請幫忙。

+1

在路線變更時使用解決方案。並使用$ httpProvider.interceptors –

+0

你能解釋一下嗎? – Naveen

+0

如果用戶通過身份驗證,返回的isLoggedIn()函數是什麼? 'if(!Auth.isLoggedIn())'行應該返回true或false。這是否發生? – sisyphus

回答

2

嘗試這樣的事:

$rootScope.$on('$routeChangeStart', function (event, next) { 
    if (next.$$route.originalPath!=='/Login' && !Auth.isLoggedIn()) { 
    ... 

否則,你會進入一個無限循環。

+0

感謝Shalom和@Manikandan的工作。 – Naveen

相關問題