Skip to content

Commit 027195e

Browse files
committed
add hooks for hash password
Signed-off-by: BinotaLIU <me@binota.org>
1 parent d99346f commit 027195e

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lib/models/user.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ module.exports = function (sequelize, DataTypes) {
5757
return false
5858
}
5959

60+
User.addHook('beforeCreate', async function (user) {
61+
// only do hash when password is presented
62+
if (user.password) {
63+
user.password = await User.hashPassword(user.password)
64+
}
65+
})
66+
User.addHook('beforeUpdate', async function (user) {
67+
if (user.changed('password')) {
68+
user.password = await User.hashPassword(user.password)
69+
}
70+
})
71+
6072
User.associate = function (models) {
6173
User.hasMany(models.Note, {
6274
foreignKey: 'ownerId',

0 commit comments

Comments
 (0)