【问题标题】:Mean.js req.isAuthenticated is showing fail?Mean.js req.isAuthenticated 显示失败?
【发布时间】:2016-02-23 21:16:54
【问题描述】:

我已经下载了meanjs version@0.1.12.here我使用了两台服务器作为前端我hvae使用angular和ionic它在localhost:3000中运行,对于后端我使用meanjs.in that meanjs我已经创建了注册,登录和文章。当我使用meansjs作为后端和前端时,它工作正常。但是当我连接到另一台服务器(本地主机:3000)注册和登录工作正常但是当我创建文章时,我得到401未经授权的bcoz那个 req.isAuthenticated() 函数。当我创建文章模块 req.isAuthenticated() 得到 fail.req.isAuthenticated() 我不知道我应该为这个函数传递什么我已经包含了我的代码任何人都可以帮助我

现在我正在传递这样的数据

    $http.post('http://192.168.1.14:3000/articles', credentials).success(function(response,data,errorResponse) {
            // If successful we assign the response to the global user model
            //$scope.authentication.user =response;
             console.log(response);

             console.log(data);
            // And redirect to the index page
            $location.path('/tab/account');
        }, function(response,data,errorResponse) {
            $scope.error = errorResponse.data.message;
            console.log($scope.error);
            console.log(data);
        });

路线:

app.route('/articles')
        .get(users.requiresLogin,articles.list)
        .post(users.requiresLogin,articles.create);

登录检查

/**
 * Require login routing middleware
 */
exports.requiresLogin = function(req, res, next) {
    //console.log(req.isAuthenticated());
    console.log(req);
    if (!req.isAuthenticated()) {
        return res.status(401).send({
            message: 'User is not logged in'
        });
    }

    next();
};

/**
 * User authorizations routing middleware
 */
exports.hasAuthorization = function(roles) {
    var _this = this;
console.log('sss');
    return function(req, res, next) {
        _this.requiresLogin(req, res, function() {
            if (_.intersection(req.user.roles, roles).length) {
                return next();
            } else {
                return res.status(403).send({
                    message: 'User is not authorized'
                });
            }
        });
    };
};

【问题讨论】:

  • 您在登录后尝试创建文章吗? req.isAuthenticated() 为您返回 false 对吗?你是用mean.js登录功能登录的吗?
  • 我只在登录后创建。我在登录后通过了凭据。是的,我正在使用 meanjs 功能。我是这项技术的新手
  • 如果您使用 mean.js 功能,那么他们正在使用 passport-local 进行身份验证。不知道为什么会收到这些错误?您清除或删除您的数据库,如果您没有更改 html 页面以创建文章,请删除您在客户端 controller.js 中声明的凭据。我会更新你的代码
  • 我收到错误,因为两台服务器前端运行一个服务器,而 menjs 运行不同的服务器。我不知道护照期望验证什么
  • req.isAuthenticated() 返回 false 还是 null?你真的安装了 Passport 或 Express Session 吗?还是您只是安装了平均堆栈?你能 console.log req.isAuthenticated() 告诉我们值是多少吗?

标签: node.js express passport.js meanjs passport-local


【解决方案1】:

我想我遇到了同样的问题。请务必检查服务器端的政策文件夹。

roles: ['user'],
allows: [{
  resources: '/articles',
  permissions: ['get', 'post']
}, {
  resources: '/articles/:articlesId',
  permissions: ['get']
}, {
  resources: '/articles',
  permissions: ['post']
}]

添加资源路径/articles和权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 2021-12-10
    • 2020-02-08
    • 2011-05-21
    相关资源
    最近更新 更多