add hooks for hash password

Signed-off-by: BinotaLIU <me@binota.org>
This commit is contained in:
BinotaLIU 2020-04-14 23:15:26 +08:00
parent d99346f037
commit 027195e973
No known key found for this signature in database
GPG Key ID: 87C590ECC1F5314E
1 changed files with 12 additions and 0 deletions

View File

@ -57,6 +57,18 @@ module.exports = function (sequelize, DataTypes) {
return false
}
User.addHook('beforeCreate', async function (user) {
// only do hash when password is presented
if (user.password) {
user.password = await User.hashPassword(user.password)
}
})
User.addHook('beforeUpdate', async function (user) {
if (user.changed('password')) {
user.password = await User.hashPassword(user.password)
}
})
User.associate = function (models) {
User.hasMany(models.Note, {
foreignKey: 'ownerId',