Skip to content
This repository was archived by the owner on May 10, 2024. It is now read-only.

Commit 7c0ab13

Browse files
committed
fix: solve change password problem
1 parent 811d93f commit 7c0ab13

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

api-service/common/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "core-components",
3-
"version": "1.1.6",
3+
"version": "1.1.10",
44
"description": "Core components that are shared between the different Javascript's services of the organization",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

api-service/common/src/models/users/users.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import mongoose, {Model, Schema, Types} from "mongoose"
1+
import mongoose, {Model, Schema, Types, UpdateQuery} from "mongoose"
22
import {MongoError} from "mongodb"
33
import bcrypt from "bcrypt"
44
import {isEmail, isStrongPassword, isAlpha} from "validator";
@@ -57,9 +57,7 @@ let User = new Schema<IUser, UserDocumentType>({
5757

5858
User.pre("save", function (next) {
5959
const user = this;
60-
61-
if (user.isModified("password") || user.isNew) {
62-
60+
if (user.isModified("password") || user.isNew) {
6361
bcrypt.genSalt(SALT_WORK_FACTOR, function (error, salt) {
6462
if (error) {
6563
return next(error)
@@ -69,7 +67,7 @@ User.pre("save", function (next) {
6967
if (error) {
7068
return next(error)
7169
}
72-
70+
7371
user.password = hash
7472
next()
7573
})
@@ -80,6 +78,32 @@ User.pre("save", function (next) {
8078
}
8179
});
8280

81+
82+
User.pre("updateOne", function(next) {
83+
const update: any = {...this.getUpdate()};
84+
const context = this;
85+
if (update.password) {
86+
bcrypt.genSalt(SALT_WORK_FACTOR, function (error, salt) {
87+
if (error) {
88+
return next(error)
89+
} else {
90+
91+
bcrypt.hash(update.password, salt, function(error, hash) {
92+
if (error) {
93+
return next(error)
94+
}
95+
96+
update.password = hash
97+
context.setUpdate(update);
98+
next()
99+
})
100+
}
101+
})
102+
} else {
103+
next();
104+
}
105+
});
106+
83107
User.post('save', function(error: Error, doc: Document, next) {
84108
if(error && error instanceof MongoError) {
85109
if (error.code === 11000) {

0 commit comments

Comments
 (0)