You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
In the src/models/User.ts file, to hash the password using bcrypt, the program is using a Pre Middleware instead of a setter for the password:
/**
* Password hash middleware.
*/
userSchema.pre("save", function save(next) {
const user = this as UserDocument;
if (!user.isModified("password")) { return next(); }
bcrypt.genSalt(10, (err, salt) => {
if (err) { return next(err); }
bcrypt.hash(user.password, salt, undefined, (err: mongoose.Error, hash) => {
if (err) { return next(err); }
user.password = hash;
next();
});
});
});
Just wondering, why is it using a middleware instead of a simple setter? I think this was done this way so that it could use asynchronous bcrypt functions, but correct me if I'm wrong. If this is the reason, this was a really good design choice.
The text was updated successfully, but these errors were encountered:
I believe this is well known pattern for Mongoose schema (pre/post hooks). This block of code will be eventually called by library,
removing a need from person implementing it to take care about lot of other details (when/why/how). So your controller actions are slim and focused on saving/validating user data model, while the rest is take out of the picture (here the salting/hashing)
You will find this in other frameworks as well (I'm more C# MVC, in ORM framework like Entity Framework this is done via framework interceptors, but can be done also via server middleware alone)
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
In the
src/models/User.ts
file, to hash the password using bcrypt, the program is using a Pre Middleware instead of a setter for the password:Just wondering, why is it using a middleware instead of a simple setter? I think this was done this way so that it could use asynchronous bcrypt functions, but correct me if I'm wrong. If this is the reason, this was a really good design choice.
The text was updated successfully, but these errors were encountered: