Passport signup-local返回“Missing Credentials”

时间:2015-06-17 09:58:22

标签: node.js passport.js

尝试使用express设置节点和护照,我偶然发现了一个问题,我调试时遇到了一些问题。

有一个非常基本的注册表单,它一直可以按下注册按钮。但是,注册不起作用。没有错误消息,但代码确实识别出注册失败(没有错误,但没有用户)。经过一段时间的努力,我设法得到了那个passport.authenticate(signup-local在信息中发送了一条消息,上面写着“Missing Credentials”,但我不知道凭证或原因。

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

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

我想看看帖子收到的内容,但不确定如何以简单的方式获得该内容:)

1 个答案:

答案 0 :(得分:2)

好的,所以经过一番调整我发现了问题所在。

您需要加载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);

    });
相关问题