2017-02-17 13 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; 
     } 
    }); 
}; 

});

これは問題なく動作します。ログインするか、新しい登録を行った後に認証が取得されます。しかし、ページをリフレッシュした後、私の認証は失われます。私はこれらの行が問題を生み出すと思います。私はこの$rootScope.authenticated = false;ラインを持っている私のmaincontroller.jsの上部に

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'; 
}; 
}); 

。これは私が知っている必要がありますが、これは爽やかな後に真ではなく偽でなければならないという行かもしれません。この問題を回避するにはどうすればよいですか?代わりにrootScope上のデータを格納するので

答えて

0

は、代わりのsessionStorageに保管してください - これはあなたのページに新しいインスタンスをリフレッシュしますたびので、リフレッシュ

https://github.com/gsklee/ngStorage

0

使用localStorageまたはsessionStorage渡って持続します$rootScopeが作成されます。

関連する問題