use bcrypt synchronously
This commit is contained in:
1 parent
fc285f7070
commit
8fec35c299
1 file changed
+9
-9
@@ -1,5 +1,5 @@
|
|||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import bcrypt from "bcrypt";
|
import bcrypt from "bcryptjs";
|
||||||
import logger from "../../utils/logger.js";
|
import logger from "../../utils/logger.js";
|
||||||
|
|
||||||
const UserSchema = mongoose.Schema(
|
const UserSchema = mongoose.Schema(
|
||||||
@@ -55,20 +55,20 @@ const UserSchema = mongoose.Schema(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
UserSchema.pre("save", async function (next) {
|
UserSchema.pre("save", function (next) {
|
||||||
if (!this.isModified("password")) {
|
if (!this.isModified("password")) {
|
||||||
next();
|
return next();
|
||||||
}
|
}
|
||||||
const salt = await bcrypt.genSalt(10); //genSalt is asynchronous, need to wait
|
const salt = bcrypt.genSaltSync(10);
|
||||||
this.password = await bcrypt.hash(this.password, salt); // hash is also async, need to eitehr await or use hashSync
|
this.password = bcrypt.hashSync(this.password, salt);
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
UserSchema.pre("findOneAndUpdate", async function (next) {
|
UserSchema.pre("findOneAndUpdate", function (next) {
|
||||||
const update = this.getUpdate();
|
const update = this.getUpdate();
|
||||||
if ("password" in update) {
|
if ("password" in update) {
|
||||||
const salt = await bcrypt.genSalt(10); //genSalt is asynchronous, need to wait
|
const salt = bcrypt.genSaltSync(10);
|
||||||
update.password = await bcrypt.hash(update.password, salt); // hash is also async, need to eitehr await or use hashSync
|
update.password = bcrypt.hashSync(update.password, salt);
|
||||||
}
|
}
|
||||||
|
|
||||||
next();
|
next();
|
||||||
@@ -89,4 +89,4 @@ User.init().then(() => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
export default mongoose.model("User", UserSchema);
|
export default User;
|
||||||
Reference in new issue
Block a user