bcryptjs compare 2021 problem

Solutions on MaxInterview for bcryptjs compare 2021 problem by the best coders in the world

showing results for - "bcryptjs compare 2021 problem"
Mads
07 Nov 2020
1Somehow it used to work before feb 2021 but afterwards it just bugged...
2
3For People like me who uses Bcryptjs for hashing / Crypting password 
4that they pass to MongoDB:
5
6If you have a:
7
8 userSchema.pre("save", async function (next) {
9	if (!this.isModified("password")) return next();
10    const salt = await bcrypt.genSalt(10);
11	this.password = await bcrypt.hash(this.password, salt);
12	next();
13 });
14
15then add a this.isNew like: 
16	
17 userSchema.pre("save", async function (next) {
18	if (!this.isModified("password") || this.isNew) return next();
19    const salt = await bcrypt.genSalt(10);
20	this.password = await bcrypt.hash(this.password, salt);
21	next();
22 });
23
24Checkout the StackOverflow answer by Samso Maosa