2017-02-17 87 views
0

所以我对我的web应用程序进行了身份验证。这是我的身份验证控制器。刷新页面后丢失身份验证(AngularJS,NodeJS)

myTodoList.controller('authController', function($scope, $rootScope, $http, $location) { 
$scope.user = { username: '', password: '' }; 
$scope.error_message = ''; 

$scope.login = function() { 
    $http.post('/auth/login', $scope.user).success(function(data) { 
     if (data.state == 'success') { 
      $rootScope.authenticated = true; 
      $rootScope.current_user = data.user.username; 
      $location.path('/app'); 
     } else { 
      $scope.error_message = data.message; 
     } 
    }); 
}; 

$scope.register = function() { 
    $http.post('/auth/signup', $scope.user).success(function(data) { 
     if (data.state == 'success') { 
      $rootScope.authenticated = true; 
      $rootScope.current_user = data.user.username; 
      $location.path('/app'); 
     } else { 
      $scope.error_message = data.message; 
     } 
    }); 
}; 

});

这工作正常,我登录后进行身份验证或进行新的注册。但刷新页面后,我失去了我的身份验证。我认为这些线路会产生问题。

var myTodoList = angular.module("myTodoList", ['ngRoute', 'ngResource']).run(function($http, $rootScope) { 
$rootScope.authenticated = false; 
$rootScope.current_user = 'Guest'; 

$rootScope.signout = function() { 
    $http.get('auth/signout'); 
    $rootScope.authenticated = false; 
    $rootScope.current_user = 'Guest'; 
}; 
}); 

在我maincontroller.js顶部我有这个$rootScope.authenticated = false;线。这是我所知道的,但这可能是这样的一句话,即在刷新之后它应该是错的而不是真的。我该怎么做才能解决这个问题?

回答

0

使用localStoragesessionStorage因为每次当你将刷新页面的新实例$rootScope将被创建。