【发布时间】:2016-11-16 23:42:42
【问题描述】:
我正在尝试使用passport-local 将一些身份验证和密码哈希烘焙到一个小型网络应用程序中。我相信我已经正确设置了它,但是即使凭据 110% 正确,我也会通过失败重定向再次登录。
护照文档对调试错误的描述非常简单,任何人都可以分享一些关于此失败重定向的可能原因,或提供继续调试的解决方案吗?
护照配置
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
},
function(username, password, cb) {
UserSchema.findByUsername(username, function(err, user) {
if (err) { return cb(err); }
if (!user) { return cb(null, false); }
if (user.password != password) { return cb(null, false); }
return cb(null, user);
});
}));
登录路径
router.post('/login',
passport.authenticate('local', {
failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
Schema 和 findByUsername 函数
const UserSchema = mongoose.Schema ({
firstName: {
type: String,
index: true
},
lastName: {
type: String,
index: true
},
email: {
type: String,
index: true
},
password: {
type: String,
index: true
},
homeAirport: {
type: String,
index: true
}
})
UserSchema.plugin(passportLocalMongoose)
// Adding this function to find by username
exports.findByUsername = function(username, cb) {
process.nextTick(function() {
for (var i = 0, len = records.length; i < len; i++) {
var record = records[i];
if (record.username === username) {
return cb(null, record);
}
}
return cb(null, null);
});
}
// Export it to the app
module.exports = mongoose.model('user', UserSchema)
【问题讨论】:
-
作为调试步骤,将用户返回函数替换为返回文字用户对象的函数。
-
另外,我在这方面有点菜鸟,但在我尝试的一个小项目中,我使用了 passportLocalMongoose,它将用户名、密码、盐等添加到我的用户模型以及找到的方法这里github.com/saintedlama/…
标签: javascript node.js mongodb passport.js passport-local