【问题标题】:Getting authentication of actual user to not ask for authentication again获取实际用户的身份验证以不再要求身份验证
【发布时间】:2017-12-06 08:23:49
【问题描述】:

所以我已经在 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的一部分,现在看起来像上面那样。

但是仍然不会首先检查用户是否已经通过身份验证并要求进行身份验证。请帮忙。

【问题讨论】:

    标签: javascript angularjs mongodb cookies


    【解决方案1】:

    所以一段时间后,我已经了解了 AgnularJS 的很多内容,并且 anwser 就这么简单,在登录时在 succesCallback 上创建一个范围,例如 $scope.authenticated = 0,并将其设置为 $scope.authenticated = 1 并将其存储在 cookie 中。

    【讨论】:

      猜你喜欢
      • 2017-05-06
      • 2021-12-09
      • 2019-12-02
      • 1970-01-01
      • 2016-04-12
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多