schema password hashing code

Solutions on MaxInterview for schema password hashing code by the best coders in the world

showing results for - "schema password hashing code"
Océane
03 Aug 2017
1UserSchema.pre('save', function(next) {
2    var user = this;
3
4    // only hash the password if it has been modified (or is new)
5    if (!user.isModified('password')) return next();
6
7    // generate a salt
8    bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
9        if (err) return next(err);
10
11        // hash the password along with our new salt
12        bcrypt.hash(user.password, salt, function(err, hash) {
13            if (err) return next(err);
14
15            // override the cleartext password with the hashed one
16            user.password = hash;
17            next();
18        });
19    });
20});