Password Reset In NodeJS

前端 未结 4 934
春和景丽
春和景丽 2021-01-31 11:15

I have made a set up to update a user\'s password using NodeJS/Passport. I followed this great guide: http://sahatyalkabov.com/how-to-implement-password-reset-in-nodejs/.

4条回答
  •  爱一瞬间的悲伤
    2021-01-31 11:22

    I already used this code in my current project, and its working fine, I saw a small error in your code in function UserSchema.pre('save', function(next). when you hash the password with bcrypt.hash then it took four arguments but there are only three argument in my code like

    schema.pre('save', function(next) {
        var user = this;
        var SALT_FACTOR = 5;
    
        if(!user.isModified('password')){
            return next();
        }
    
        bcrypt.genSalt(SALT_FACTOR, function(err, salt) {
            if(err){
                return next(err);
            }
            bcrypt.hash(user.password, salt, function(err, hash) {
                if(err){
                    return next(err);
                }
                user.password = hash;
                next();
            });
        });
    });
    

    Third argument must be callback function see document for bcrypt

提交回复
热议问题