In typeorm, I am trying to use a subscriber decorator to hash users password before persisting to the database. Unfortunately, I can't find a reference in the docs.
In sequelizejs, I use the following code,
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));
};
Right now, I am trying to migrate the code to typeorm
and my translation is roughly
@BeforeInsert()
@BeforeUpdate()
hashPassword() {
// conditional to detect if password has changed goes here
this.password = bcrypt.hashSync(this.password, SALT_ROUNDS);
}
The issue is, I stuck at !user.changed('password')
. Is there an equivalent function in typeorm
to do this without rolling out my own solution?
user.password
property is present in the update request. If it is present, confirm that it is a plain string to prevent a double hash. Then manually run thebcrypt.hash()
on the plain string before persisting. – adetoola