This is how my controller looks like:
const user = await auth.getUser()
user.profile_pic = 'somethingnew.png'
await user.save()
return user
When I do this the hashPassword hook hashes the old password. How can I prevent this?
It depends on your Password Reset strategy, if you allow your user to reset password directly in profile, then you may call new password field as new_password and check if it exists on update in controller, then Hash it in controller. Doing this you can remove password hashing from beforeUpdate hooks.
On my hash password hook I check if the password on the user model is in the dirty fields before hashing it, otherwise leave it as it is
Yes, exactly as @webdevian said. You can check for $dirty fields.
UserHook.hashPassword = async (userInstance) => {
if (userInstance.$dirty.password) {
userInstance.password = await Hash.make(userInstance.$dirty.password)
}
}
I couldn't find this in the docs
i could only get it working without the $
UserHook.hashPassword = async (userInstance) => {
// without the $
if (userInstance.dirty.password) {
userInstance.password = await Hash.make(userInstance.password)
}
}
and i also think this should be the default behaviour
Most helpful comment
Yes, exactly as @webdevian said. You can check for
$dirtyfields.