所以我在angularjs中对用户进行了身份验证。我正在部署MongoDB。我可以将用户存储在cookie中,但我试图将身份验证存储在cookie中,因为每次我尝试访问用户面板时都会询问用户名和密码,但用户已经存储在cookie中。当我从cookie中获取用户时,它仍然要求我再次登录。
部分我的adminController.js
认证的一部分:
.controller("authCtrl", function($scope, $http, $location, authUrl) {
$scope.authenticate = function (login, pass) {
$http.get('http://localhost:5500/users/me')
$http.post(authUrl, {
username: login,
password: pass
}, {
withCredentials: true
}).success(function (data) {
$location.path("/main");
}).error(function (error) {
$scope.authenticationError = error;
});
};
})
我的cuurentUserController.js
:
angular.module("sportsStoreAdmin")
.config(function($httpProvider){
$httpProvider.defaults.withCredentials = true;
})
.controller("currentCtrl" ,function ($http, $scope, $cookieStore) {
$http.get('http://localhost:5500/users/me')
.success(function (user) {
$scope.user = user;
$cookieStore.put('user', user);
});
});
登录后cookies:
%7B%22username%22%3A%22user%22%2C%22id%22%3A%228616b29237ade8b0%22%7D
因此,您可以看到已登录的用户存储在Cookie中,但仍然要求我一次又一次地登录。
我知道adminController.js
的这一部分没有被提及,因为它只获得了实际用户,但没有让他进行身份验证:
$cookieStore.get('user')
我该怎么做才能让它发挥作用?
修改
所以我尝试了更多的东西,并在deployed
文档中找到了我可以通过使用此代码来代替$cookieStore.get('user')
获得身份验证的实际用户:
$http.get('http://localhost:5500/users/me')
所以我编辑了adminController.js
的一部分,现在看起来就像上面一样。
但仍然没有首先检查用户是否已经过身份验证并要求进行身份验证。请帮忙。
答案 0 :(得分:0)
所以经过一段时间我已经学习了很多AgnularJS并且anwser很简单,在登录时在succesCallback上创建示例$scope.authenticated = 0
的范围,将其设置为$scope.authenticated = 1
并将其存储在cookie中。