【问题标题】:Authentification fail in bad request : Missing Credentials错误请求中的身份验证失败:缺少凭据
【发布时间】:2014-05-08 06:44:11
【问题描述】:

我正在开发 Passport Node.js,Express 登录页面。 我是一名 Web 开发人员,但这是我的第一个 Node.js 项目。

我已关注User Authentication with Passport.js

但每次我尝试登录登录页面或注册页面时,我唯一看到的就是“错误请求”页面。

我回电看看发生了什么:

exports.postRegister = function(req, res) {
    login = mongoose.model('login');
    login.register(new login({
        username: req.body.email
    }), req.body.password, function(err, login) {
        if (err) {
            return res.render('register', {
                login: login,
                error: err,
                title: "S'enregistrer"
            });
        }
        // Something is wrong here 
        passport.authenticate('local', function(err, user) {
            // So let's console log the thing !
            console.log(arguments);
        })(req, res, function() {
            res.redirect('/scene/1');
        });
    });
};

然后console.log (arguments) 返回:

{
  '0': null,
  '1': false,
  '2': { message: 'Missing credentials' },
  '3': 400
}

我已经检查了我的 MongoDB,用户创建没有问题!

【问题讨论】:

  • 对我来说完全一样的事情......同样的tuto。你找到路了吗?
  • 是的,我只是删除了护照猫鼬插件并手动完成所有实现:P
  • 我找到了这个scotch.io/tutorials/javascript/…。到目前为止效果很好

标签: node.js mongodb express passport.js


【解决方案1】:

如果您想查看它是来自 POSTMAN 还是来自您的代码,您可以在终端中使用 curl 进行测试:

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"username": "test", "password": "test"}' http://localhost:3000/api/login

【讨论】:

  • 问题是猫鼬插件!我刚刚删除了它,手动编码登录,它就像一个魅力:P
【解决方案2】:

您似乎没有为您的 login 模型指定架构。你需要像这样声明模型:

var Model = mongoose.model('Model', 'modelSchema');

【讨论】:

  • 我知道,这是因为我使用了护照插件到 mongo (Account.plugin(passportLocalMongoose);) 在官方教程中不起作用! mherman.org/blog/2013/11/11/…
猜你喜欢
  • 2014-07-08
  • 2015-06-01
  • 1970-01-01
  • 2019-09-12
  • 1970-01-01
  • 2020-12-09
  • 2021-09-24
  • 2014-01-05
  • 2014-01-20
相关资源
最近更新 更多