已经为此工作了几个小时,非常令人沮丧......
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/