diff options
author | 2022-05-29 17:45:54 +0100 | |
---|---|---|
committer | 2022-05-29 17:45:54 +0100 | |
commit | 308d07785f811ff470d0e90b11680926a823027b (patch) | |
tree | 7afe7859fc29f6a5c34be8b7b486cc317cc82e21 /models | |
parent | 50d781d0bdcac217f2bc037abe087a00019edba5 (diff) | |
download | my_apps-308d07785f811ff470d0e90b11680926a823027b.tar.gz my_apps-308d07785f811ff470d0e90b11680926a823027b.tar.bz2 my_apps-308d07785f811ff470d0e90b11680926a823027b.zip |
add change password option
Diffstat (limited to 'models')
-rw-r--r-- | models/User.js | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/models/User.js b/models/User.js index bcf2523..4992b20 100644 --- a/models/User.js +++ b/models/User.js @@ -97,6 +97,29 @@ userSchema.statics.saveSettings = async function ({ _id, theme, language }) { return userResponse(user) } +userSchema.statics.savePassword = async function ({ _id, currentPassword, newPassword }) { + const user = await User.findOne({ _id }) + + if (!user) { + throw new Error('Unable to change password') + } + + const isMatch = await bcrypt.compare(currentPassword, user.password) + + if (!isMatch) { + throw new Error('Wrong password') + } + + const password = await bcrypt.hash(newPassword, 8) + const newUser = await User.findOneAndUpdate({ _id }, { password }, { new: true }) + + if (!newUser) { + throw new Error('Could not update password') + } + + return userResponse(newUser) +} + userSchema.statics.state = async function (_id) { const user = await User.findOne({ _id }) |