2017-01-23 130 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