为什么密码和盐自动在平均值?

在这里,我试图validation用户模块中的手机号码。 我创build了令牌,并发送给用户,但每当用户尝试使用该特定令牌进行validation时,“密码”和“盐”会自动更改。 如何避免这一点? 有人帮我出来..在这里我只想更新

user.Mobileverification = 'verfied'; user.Mobileverificationcode = undefined; user.mobileVerificationExpires = undefined; 

以上三个variables被改变,但我不知道密码和盐为什么改变了?

我在下面给出了我的路线:

 app.route('/auth/mobilereset/:token').get(users.mobileresetResetToken); app.route('/auth/mobilereset/:token').post(users.mobilereset); 

控制器:

 exports.mobileresetResetToken = function(req, res) { User.findOne({ Mobileverificationcode :req.params.token, mobileVerificationExpires: { $gt: Date.now() } // resetPasswordToken: req.params.token, // resetPasswordExpires: { // $gt: Date.now() // } }, function(err, user) { if (!user) { res.send({ message: 'Invalid token' }); } else { console.log('working fine'); } }); }; exports.mobilereset = function(req, res, next) { async.waterfall([ function(done) { User.findOne({ Mobileverificationcode: req.params.token, mobileVerificationExpires: { $gt: Date.now() } }, function(err, user) { if (!err && user) { user.Mobileverification = 'verfied'; user.Mobileverificationcode = undefined; user.mobileVerificationExpires = undefined; user.save(function(err) { if (err) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); } else { req.login(user, function(err) { if (err) { res.status(400).send(err); } else { // Return authenticated user res.json(user); done(err, user); } }); } }); } else { return res.status(400).send({ message: 'reset token is invalid or has expired.' }); } }); }, ], function(err) { if (err) return next(err); }); }; 

模型:

 var UserSchema = new Schema({ username: { type: String, unique: 'testing error message', required: 'Please fill in a username', trim: true }, password: { type: String, default: '', // validate: [validateLocalStrategyPassword, 'Password should be longer'] }, email: { type: String, trim: true, default: '', // validate: [validateLocalStrategyProperty, 'Please fill in your email'], // match: [/.+\@.+\..+/, 'Please fill a valid email address'] }, Mobilenumber: { type: String, default: '' }, roles: { type: [{ type: String, enum: ['user', 'admin'] }], default: ['user'] }, salt: { type: String }, provider: { type: String, required: 'Provider is required' }, providerData: {}, additionalProvidersData: {}, updated: { type: Date }, created: { type: Date, default: Date.now }, /* For reset password */ Mobileverificationcode: { type: String, }, mobileVerificationExpires: { type: Date }, Mobileverification: { type: String, trim: true, default: 'Not Verified', }, resetPasswordToken: { type: String }, resetPasswordExpires: { type: Date } }); 

我不知道你是否删除了这个,但是在MEAN.js用户模型中,你必须小心下面的代码块:

 /** * 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(); }); 

在保存用户数据之前,将会调用它。 这可能是为什么密码和salt不断变化…你在mobile.reset()中调用user.save,上面的代码块仍然存在。

更新:一个可能的方法是:

 /** * 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(); }); 

但是可能需要进行一些调整,例如根据您的需要改进此预存挂钩,并testing密码更改和移动validation以查看是否没有任何损坏。