passport.js - Passport js缺少凭据

标签 passport.js passport-local

已经为此工作了几个小时,非常令人沮丧......

router.post('/',
passport.authenticate('local-signup', function(err, user, info) {
    console.log(err);
}), function(req, res){
    console.log(req);
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ a: 1 }));
});

当我运行它时,我使用了 console.log ,输出为 { message: 'Missing credentials' } ,这让我相信正文解析器没有正确解析正文消息。然而,当我使用这条路线时......
router.post('/',
    function(req, res){
        console.log(req.body);
        res.setHeader('Content-Type', 'application/json');
        res.send(JSON.stringify({ a: 1 }));
    });

当我使用 console.log ,输出为 {password: 'password', email: 'email@email.com'} ,这表明 req.body变量设置正确并且可用。

应用程序.js
var express = require('express');
var app = express();
var routes = require("./config/routes.config");
var models = require("./config/models.config");
var session = require('express-session');
var bodyParser = require('body-parser');

models.forEach(function(model){
    GLOBAL[model] = require('./models/'+model+".model");
});
var passport = require("./config/passport.config");

app.use( bodyParser.urlencoded({ extended: true }) );
app.use(session({ secret: 'simpleExpressMVC', resave: true, saveUninitialized: true  }));
app.use(passport.initialize());
app.use(passport.session());

最佳答案

我看到你的 req.body包含 {password: 'password', email: 'email@email.com'} . email不是passportjs要找的,而是username是。您可以更改 HTML/JS 上的输入名称,也可以更改passportjs 在req.body 中查找的默认参数。 .您需要在定义策略的地方应用这些更改。

passport.use(new LocalStrategy({ // or whatever you want to use
    usernameField: 'email',    // define the parameter in req.body that passport can use as username and password
    passwordField: 'password'
  },
  function(username, password, done) { // depending on your strategy, you might not need this function ...
    // ...
  }
));

关于passport.js - Passport js缺少凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34511021/

相关文章:

javascript - Node Express passport (JWT) - 授权后回调

javascript - 如何在单个快速服务器文件中使用两个单独的 Passport 实例

node.js - 如何使用JWT和Passport正确使用nodeJs API的身份验证?

node.js - 未找到传递参数的 Node 中的路径

javascript - 在 sails.js 下使用 SSL 时 Passport.js 无法初始化

javascript - Nodejs Q Promise catch 从未用 Passport js 调用

node.js - 理解 Passport 本地策略功能nodejs的问题

passport.js - LocalAuthGuard 无法在带有 typeorm 和 Passport-local 的 Nestjs 应用程序中工作

node.js - TypeError : schematype.castForQueryWrapper不是一个函数

node.js - 无法使用带有 Passport 本地nodejs的电子邮件进行身份验证..尝试了几个示例