已经为此工作了几个小时,非常令人沮丧......
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 protected]'}
,这表明req.body
变量已正确设置并且可用。
app.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 protected]'}
。 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 ...
// ...
}
));
在 Router.post 中:
router.post('/', passport.authenticate('local-signup', {
successRedirect: '/dashboad',
failureRedirect: '/',
badRequestMessage: 'Your message you want to change.', //missing credentials
failureFlash: true
}, function(req, res, next) {
...
我知道这里有很多答案,但这已经很清楚了。
来自官方护照文件
“默认情况下,LocalStrategy 希望在参数中查找凭据 命名的用户名和密码。如果您的网站更喜欢命名这些字段 不同的是,可以使用选项来更改默认值。”
因此,如果您发现自己遇到此问题,您有两种解决方案(最有可能)。
将前端中的身体参数重命名为
username
和 password
使用以下代码定义您的护照实例如何命名它们:
passport.use(new LocalStrategy({
usernameField: 'email', //can be 'email' or 'whateveryouwant'
passwordField: 'passwd' //same thing here
},
function(username, password, done) {
// ...
}
));
字段类型可能有错误,这就是护照.js 无法识别正确字段并给出错误的原因。
更正:如果您的用户名和密码字段正确说明了类型,请检查 .ejs 文件。
type="password" id="password" name="password"
//正确指定。
你可能会做的是:在ejs文件中
Type:"text"
并告诉护照寻找type:"password"
提示:检查类型字段中的拼写错误。 例如:ejs 中的
type:password
和 type:Password
就我自己而言,造成这种情况的原因是我的正文解析器或快速配置,我的应用程序未收到表单的输入。所以我这样做了:
// configuring express
app.use(express.static(path.join(__dirname, "public")));
// body-parser
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
就我而言,我只是错过了将
usernameField
拼写为 usernameFiled
。
确保拼写正确
usernameField
和 passwordField
。
我的是拼写错误问题。 我已经
usernameFeild: 'email',
不过应该是
usernameField: 'email',
如果您使用 Passport local mongoose 作为您的用户模型的插件。
试试这个:
passport.use(new LocalStrategy({
usernameField: 'email'
}, User.authenticate()));
在我的例子中添加配置作品中的所有字段:
passport.use(new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true
}))
就我而言,我在使用自定义字段时遇到了问题 这是我之前的代码:
const customFields = {
email: 'email',
password: 'password',
}
const verifyCallback = (email, password, done) => {
connection.query('SELECT * FROM user WHERE email = ?', email, (err, results) => {
if (err) {
return done(err);
}
if (!(results.length)) {
return done(null, false);
}
const user = results[0];
bcrypt.compare(password, user.password, (err, res) => {
if (res) {
return done(null, user);
} else {
return done(null, false);
}
});
}
)
}
const strategy = new LocalStrategy(customFields, verifyCallback);
问题是我使用了自定义字段,这完全没问题,但你需要做的是告诉护照你想更改哪个字段,以及将其更改为什么
这是之后的代码:
const customFields = {
usernameField : 'email',
passwordField : 'password',
}
const verifyCallback = (email, password, done) => {
connection.query('SELECT * FROM user WHERE email = ?', email, (err, results) => {
if (err) {
return done(err);
}
if (!(results.length)) {
return done(null, false);
}
const user = results[0];
bcrypt.compare(password, user.password, (err, res) => {
if (res) {
return done(null, user);
} else {
return done(null, false);
}
});
}
)
}
const strategy = new LocalStrategy(customFields, verifyCallback);
所以就我而言,我想使用电子邮件而不是用户名,所以我告诉护照将“usernameField”替换为“email”
我希望这会有所帮助