2017-01-23 12 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私のコードはエラーを出すことなく動作を停止します。

私は新しい角度のために何か愚かなことをしているかもしれません。 助けてください。そうしないとあなたが無限ループに入る

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

+1

ルート変更時に解決を使用します。 $ httpProvider.interceptors –

+0

を使用してさらに説明できますか? – Naveen

+0

ユーザが認証された場合に返される 'isLoggedIn()'関数は何ですか? 'if(!Auth.isLoggedIn())'の行はtrueかfalseを返すべきです。それは起こっていますか? – sisyphus

答えて

2

はこのような何かを試してみてください。

+0

ありがとうShalomと@Manikandanそれは働いた。 – Naveen

関連する問題