gpt4 book ai didi

node.js - Passport js本地策略自定义回调 "Missing credentials"

转载 作者:搜寻专家 更新时间:2023-10-31 22:35:32 24 4
gpt4 key购买 nike

我在使用 Node js、express 4 和 passport 时遇到了一个非常烦人的问题。我已经尝试了所有我能想到的选项,但仍然没有解决方案。无法从其他帖子中找到此问题的解决方案。

代码:

app.post('/login', function(req, res, next) {

console.log(req.body.user_email);
console.log(req.body.user_password);

passport.authenticate('local', function(err, user, info) {

console.log(info);
...

问题是出于某种原因,passport 没有获得凭据并在 console.log(info) 中显示“缺少凭据”,尽管在上面记录的用户名和密码是正确的。本地策略也要配置好:

passport.use(new LocalStrategy ({

usernameField: 'user_email',
passwordField: 'user_password',
},

function(username, password, done) {

console.log(username);
console.log(password);
...

“函数(用户名、密码、完成)”永远不会运行,因为“缺少凭据”。

有趣的是,从另一个 html/ejs 页面调用身份验证:

app.post('/login_user', passport.authenticate('local', { successRedirect: '/loginSuccess', failureRedirect: '/loginFailure' }));

问题不存在

有人知道我在这里错过了什么吗?!?!

谢谢!

最佳答案

如何以 HTML 格式格式化数据

我在这里使用 Postman证明您的问题:

enter image description here这不好

Just the output that I want只是我想要的输出

不关闭 (req, res)

虽然这可能没有一点关系,但如果我不这样做,也会出现同样的问题。

  1. 在启动 passport.authenticate 之前调用 req 和 res 参数
  2. 关闭passport.authenticate后,用(req, res)完成它

您需要在函数末尾使用 (req, res),如下所示:

router.post('/signup', 
(req, res, next) => passport.authenticate('whatever-your-strategy-name-is', function(err, user, info) {

// Just to see the sample expected output
res.send(JSON.stringify(info)).status(200)

})(req, res)
);

在标准的熟悉的 JS 中,它看起来像这样。

router.post('/signup', function (req, res, next) {

passport.authenticate('whatever-your-strategy-name-is', function(err, user, info) {

// Just to see the sample expected output
res.send(JSON.stringify(info)).status(200)

})(req, res) // passport.authenticate ends here
})

关于node.js - Passport js本地策略自定义回调 "Missing credentials",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30145018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com