我无法从数据库中获取哈希值和盐值。它们将在注册期间存储,但不会从应用程序中检索。下面的附件是数据库的快照,控制台屏幕,该屏幕显示检索到的用户信息(不包含salt和hash值)以及用户模式和用于获取数据的代码。
控制台上相同用户数据的快照注意:在验证电子邮件后,isVerified更改为true(在验证帐户后拍摄数据库的快照)< / em>
用于验证用户的架构方法
UserSchema.methods.validPassword = function(password) {
var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, `sha512`).toString(`hex`);
if(this.hash === hash){
return true;
}
return false;
};
下面是我的用户架构
const UserSchema = mongoose.Schema({
username: {
type: String,
required: true
},
email: {
type: String,
unique: true,
required: true
},
roles: {
type: 'String',
default: 'user'
},
isVerified: {
type: Boolean,
default: false
},
hash: String,
salt: String,
token: String,
passwordResetToken: String,
passwordResetExpires: Date
});
用于获取用户的代码
User.findOne({
username: req.body.username
}, (err, user) => {
console.log(user.validPassword(req.body.password) + " <<<<-------validPassword method result");
if (user === null) {
req.flash("error", "The provided username is incorrect. Please try again");
res.redirect("/login");
}
if (user.isVerified) {
if (user.validPassword(req.body.password)) {
res.redirect("/");
} else {
req.flash("error", "Incorrect password. Please try again");
res.redirect("/login");
}
} else if (!user.isVerified) {
console.log("user not verified");
req.flash("error", "Please confirm your email first by clicking on the activation link that was sent to you during registration");
res.redirect("/login");
}
});
错误是
“ salt”参数必须是字符串,Buffer,TypedArray或DataView类型之一。收到的类型未定义。
我相信这是因为没有从数据库中获取盐。