chore(hook): move beforeCreate hook to user model (#16)
This commit is contained in:
parent
27f508468d
commit
3735f11f91
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue
Block a user