gpt4 book ai didi

node.js - Passport 注册本地返回 "Missing Credentials"

转载 作者:太空宇宙 更新时间:2023-11-03 23:37:00 25 4
gpt4 key购买 nike

尝试使用express设置 Node 和通行证,我偶然发现了一个问题,我在调试时遇到了一些问题。

有一个非常基本的注册表单,直到按下注册按钮为止。但是,注册不起作用。没有错误消息,但代码确实识别出注册失败(不是有错误,而是没有用户)。经过一番努力后,我设法得到了 Passport.authenticate(signup-local 在 info 中发送一条消息,上面写着“缺少凭据”,但我不知道什么凭据或为什么。

我在帖子中使用的返回代码是:

app.post('/signup', function(req, res, next) {
passport.authenticate('local-signup', function(err, user, info) {
if (err) {
return next(err); // will generate a 500 error
}
// Generate a JSON response reflecting authentication status
if (! user) {
console.log('Got the following back');
console.log('Error: ' + err);
console.log('User: ' + user);
info = JSON.stringify(info);
console.log('Info: '+ info);
for(var key in info) {
console.log(key);
}
return res.send({ success : false, message : 'authentication failed'});
}
return res.send({ success : true, message : 'authentication succeeded' });
})(req, res, next);

});

返回结果是:

GET /signup 304 24.943 ms - -
Got the following back
Error: null
User: false
Info: {"message":"Missing credentials"}
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
POST /signup 200 6.933 ms - 51

我想看看帖子收到了什么,但不确定如何以简单的方式获得它:)

最佳答案

好的,经过一番摆弄,我弄清楚了问题所在。

您需要加载 urlencodedParser。因此将以下内容添加到路线的开头

var bodyParser = require('body-parser');

module.exports = function(app, passport) {

var urlencodedParser = bodyParser.urlencoded({ extended: false })

完成后,您所要做的就是通过 urlencodedParser 和 Bobs 你叔叔传递调用

  app.post('/signup', urlencodedParser, function(req, res, next) {
passport.authenticate('local-signup', function(err, user, info) {
if (err) {
return next(err); // will generate a 500 error
}
// Generate a JSON response reflecting authentication status
if (! user) {
console.log('Got the following back');
console.log('Error: ' + err);
console.log('User: ' + user);
info = JSON.stringify(info);
console.log('Info: '+ info);
for(var key in info) {
console.log(key);
}
var userd = JSON.stringify(req.body);
console.log('Request : ' + userd);
return res.send({ success : false, message : 'authentication failed'});
}
return res.send({ success : true, message : 'authentication succeeded' });
})(req, res, next);

});

关于node.js - Passport 注册本地返回 "Missing Credentials",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30888265/

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