Why Password and Salt automatically in MEAN STACK? Why Password and Salt automatically in MEAN STACK? mongoose mongoose

Why Password and Salt automatically in MEAN STACK?


I don't know if you removed this or not but in MEAN.js user model, you have to be careful with the following code block:

/** * Hook a pre save method to hash the password */UserSchema.pre('save', function (next) {   if (this.password && this.isModified('password')) {     this.salt = crypto.randomBytes(16).toString('base64');    this.password = this.hashPassword(this.password);   }  next();});

Which will be called right before you save the user data. That's probably why password and salt keep changing... You are calling user.save in mobile.reset() and that code block above is still present somewhere.

Update:A possible way of doing it is:

/** * Hook a pre save method to hash the password */UserSchema.pre('save', function (next) {   if(!this.isModified('Mobileverification') && !this.isModified('Mobileverificationcode') && !this.isModified('mobileVerificationExpires')) {       if (this.password && this.isModified('password')) {         this.salt = crypto.randomBytes(16).toString('base64');         this.password = this.hashPassword(this.password);       }    }  next();});

However it might need a few adjustments, such as improving this pre-save hook according to your needs and testing password changing and mobile verification to see if nothing is broken.