chore(hook): move beforeCreate hook to user model (#16)

This commit is contained in:
Romain Lanz 2018-04-10 17:17:47 +02:00 committed by Harminder
parent 27f508468d
commit 3735f11f91
2 changed files with 6 additions and 24 deletions

View File

@ -1,20 +0,0 @@
'use strict'
const Hash = use('Hash')
const UserHook = module.exports = {}
/**
* Hash using password as a hook.
*
* @method
*
* @param {Object} userInstance
*
* @return {void}
*/
UserHook.hashPassword = async (userInstance) => {
if (userInstance.password) {
userInstance.password = await Hash.make(userInstance.password)
}
}

View File

@ -1,5 +1,6 @@
'use strict'
const Hash = use('Hash')
const Model = use('Model')
class User extends Model {
@ -9,11 +10,12 @@ class User extends Model {
/**
* A hook to hash the user password before saving
* it to the database.
*
* Look at `app/Models/Hooks/User.js` file to
* check the hashPassword method
*/
this.addHook('beforeCreate', 'User.hashPassword')
this.addHook('beforeCreate', async (userInstance) => {
if (userInstance.password) {
userInstance.password = await Hash.make(userInstance.password)
}
})
}
/**