【发布时间】:2019-06-28 04:25:42
【问题描述】:
我正在尝试使用打字稿在猫鼬模型中添加 bcrypt。 我以this link 为例。
但我的项目使用的是打字稿,所以我不能完全使用相同的代码。我不明白他们从哪里获得密码来与用户发送密码进行匹配。
当我比较密码时,似乎其中一个密码未定义。 以下是我的代码,不胜感激。
PersonTestSchema.pre<PersonTestModel>('save', function (next) {
const user = this;
if (this.password && this.password.length > 4) {
bcrypt.genSalt(10, function (err, salt) {
bcrypt.hash(user.password, salt, (err, hash) => {
user.password = hash;
next();
});
});
} else {
next();
}
});
PersonTestSchema.methods.verifyPassword = function (candidatePassword: string) {
const user = this;
return bcrypt.compareSync(candidatePassword, user.password);
当我记录候选密码时,它会被填满,但如果我记录 user.password 它是空的。 这在我的代码中似乎是合乎逻辑的,但我不明白他们在示例代码中从哪里得到它。
【问题讨论】:
标签: typescript express mongoose bcrypt