remove mongo hash middleware
This commit is contained in:
1 parent
7e527b8eb2
commit
2e3b32462f
2 files changed
+17
-23
No files matched your search
@@ -50,24 +50,6 @@ const UserSchema = new Schema<UserDocument>(
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
UserSchema.pre("save", function (next) {
|
||||
if (!this.isModified("password")) {
|
||||
return next();
|
||||
}
|
||||
const salt = bcrypt.genSaltSync(10);
|
||||
this.password = bcrypt.hashSync(this.password, salt);
|
||||
next();
|
||||
});
|
||||
|
||||
UserSchema.pre("findOneAndUpdate", function (next) {
|
||||
const update = this.getUpdate();
|
||||
if (update && "password" in update) {
|
||||
const salt = bcrypt.genSaltSync(10);
|
||||
(update as any).password = bcrypt.hashSync((update as any).password, salt);
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
UserSchema.pre("findOneAndDelete", async function (next) {
|
||||
try {
|
||||
const userToDelete = await this.model.findOne(this.getFilter());
|
||||
|
||||
@@ -16,6 +16,11 @@ const SERVICE_NAME = "userService";
|
||||
class UserService {
|
||||
static SERVICE_NAME = SERVICE_NAME;
|
||||
|
||||
private hashPassword = (password: string): string => {
|
||||
const salt = bcrypt.genSaltSync(10);
|
||||
return bcrypt.hashSync(password, salt);
|
||||
};
|
||||
|
||||
private emailService: any;
|
||||
private settingsService: any;
|
||||
private logger: any;
|
||||
@@ -103,7 +108,12 @@ class UserService {
|
||||
user.role = ["superadmin"];
|
||||
}
|
||||
|
||||
const newUser = await this.usersRepository.create({ ...user }, file);
|
||||
// Hash password before storing
|
||||
if (user.password) {
|
||||
user.password = this.hashPassword(user.password);
|
||||
}
|
||||
|
||||
const newUser = await this.usersRepository.create(user, file);
|
||||
|
||||
this.logger.debug({
|
||||
message: "New user created",
|
||||
@@ -182,7 +192,8 @@ class UserService {
|
||||
throw new AppError({ message: "Incorrect current password", service: SERVICE_NAME, status: 403 });
|
||||
}
|
||||
// If a match, update the password
|
||||
updates.password = updates.newPassword;
|
||||
updates.password = this.hashPassword(updates.newPassword);
|
||||
delete updates.newPassword;
|
||||
}
|
||||
|
||||
return await this.usersRepository.updateById(currentUser.id, updates, file);
|
||||
@@ -225,8 +236,8 @@ class UserService {
|
||||
throw new AppError({ message: "New password cannot be same as old password", service: SERVICE_NAME, status: 400 });
|
||||
}
|
||||
|
||||
existingUser.password = password;
|
||||
await this.usersRepository.updateById(existingUser.id, existingUser, null);
|
||||
const hashedPassword = this.hashPassword(password);
|
||||
await this.usersRepository.updateById(existingUser.id, { password: hashedPassword }, null);
|
||||
await this.recoveryTokensRepository.deleteManyByEmail(existingUser.email);
|
||||
|
||||
existingUser.password = "";
|
||||
@@ -294,7 +305,8 @@ class UserService {
|
||||
};
|
||||
|
||||
setPasswordByUserId = async (userId: any, password: string) => {
|
||||
const updatedUser = await this.usersRepository.updateById(userId, { password }, null);
|
||||
const hashedPassword = this.hashPassword(password);
|
||||
const updatedUser = await this.usersRepository.updateById(userId, { password: hashedPassword }, null);
|
||||
return updatedUser;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user