gpt4 book ai didi

node.js - 为什么密码和盐会自动出现在 MEAN STACK 中?

转载 作者:太空宇宙 更新时间:2023-11-03 22:30:04 24 4
gpt4 key购买 nike

这里我尝试验证用户模块中的手机号码。我已经创建了 token 并将其发送给用户,但每当用户尝试使用该特定 token 进行验证时,“密码”和“盐”都会自动更改。如何避免这种情况?有人帮助我..这里我只想更新

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

以上三个变量发生了变化,但我不知道为什么密码和盐发生了变化?

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

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

Controller :

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

它将在保存用户数据之前调用。这可能就是密码和盐不断变化的原因......您在 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();
});

但是,它可能需要一些调整,例如根据您的需要改进此预保存 Hook ,并测试密码更改和移动验证以查看是否没有任何损坏。

关于node.js - 为什么密码和盐会自动出现在 MEAN STACK 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38410304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com