【发布时间】:2019-01-12 19:49:36
【问题描述】:
在 typeorm 中,我尝试使用订阅者装饰器在持久化到数据库之前对用户密码进行哈希处理。不幸的是,我在文档中找不到参考。
在sequelizejs中,我使用如下代码,
User.hashPassword = (user, options) => {
if (!user.changed('password')) {
return null;
}
// hash password
return Bcrypt.hash(user.get('password'), SALT_ROUNDS)
.then(hash => user.set('password', hash));
};
现在,我正在尝试将代码迁移到typeorm,我的翻译大致是
@BeforeInsert()
@BeforeUpdate()
hashPassword() {
// conditional to detect if password has changed goes here
this.password = bcrypt.hashSync(this.password, SALT_ROUNDS);
}
问题是,我停留在!user.changed('password')。 typeorm 中是否有等效功能可以在不推出我自己的解决方案的情况下执行此操作?
【问题讨论】:
-
你找到解决办法了吗?
-
@Hammerbot 不,我没有。我所做的是检查更新请求中是否存在
user.password属性。如果存在,请确认它是纯字符串以防止双重哈希。然后在持久化之前在纯字符串上手动运行bcrypt.hash()。 -
您是否尝试过使用订阅者? github.com/typeorm/typeorm/blob/master/docs/…
标签: javascript node.js nestjs typeorm