aboutsummaryrefslogtreecommitdiffstats
path: root/models/User.js
diff options
context:
space:
mode:
Diffstat (limited to 'models/User.js')
-rw-r--r--models/User.js23
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 })