刷新页面后失去身份验证(AngularJS,NodeJS)

时间:2017-02-17 11:51:54

标签: angularjs node.js express

所以我对我的网络应用程序进行了身份验证。这是我的身份验证控制器。

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;行。我知道这是必要的,但这可能是说明它应该是假的,而不是刷新后的真实。我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:0)

不是将数据存储在rootScope上,而是将其存储在sessionStorage中 - 这将在刷新时保留

https://github.com/gsklee/ngStorage

相关问题