From 3735f11f9115976b5687fac96ce8ff3189c8f4a0 Mon Sep 17 00:00:00 2001 From: Romain Lanz Date: Tue, 10 Apr 2018 17:17:47 +0200 Subject: [PATCH] chore(hook): move beforeCreate hook to user model (#16) --- app/Models/Hooks/User.js | 20 -------------------- app/Models/User.js | 10 ++++++---- 2 files changed, 6 insertions(+), 24 deletions(-) delete mode 100644 app/Models/Hooks/User.js diff --git a/app/Models/Hooks/User.js b/app/Models/Hooks/User.js deleted file mode 100644 index 4843f1c..0000000 --- a/app/Models/Hooks/User.js +++ /dev/null @@ -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) - } -} diff --git a/app/Models/User.js b/app/Models/User.js index cfc30b6..265968c 100644 --- a/app/Models/User.js +++ b/app/Models/User.js @@ -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) + } + }) } /**