feat: Ask for current password while updating user password

This commit is contained in:
Faruk AYDIN
2024-09-12 12:54:46 +03:00
parent 8bd66da511
commit 32d39b88bd
3 changed files with 44 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
import bcrypt from 'bcrypt';
import { DateTime, Duration } from 'luxon';
import crypto from 'node:crypto';
import { ValidationError } from 'objection';
import appConfig from '../config/app.js';
import { hasValidLicense } from '../helpers/license.ee.js';
@@ -249,6 +250,27 @@ class User extends Base {
});
}
async updatePassword({ currentPassword, password }) {
if (await User.authenticate(this.email, currentPassword)) {
const user = await this.$query().patchAndFetch({
password,
});
return user;
}
throw new ValidationError({
data: {
currentPassword: [
{
message: 'is incorrect.',
},
],
},
type: 'ValidationError',
});
}
async softRemove() {
await this.softRemoveAssociations();
await this.$query().delete();