【问题标题】:How to update salt and hash value in mongoDB when user changes the password in nodejs?当用户在nodejs中更改密码时如何更新mongoDB中的盐和哈希值?
【发布时间】:2021-02-05 18:11:45
【问题描述】:

我必须为用户实现一项功能,该用户可以在登录后将密码更改为新密码。 为此,我想更新新密码集的 hashsalt 值。我怎样才能做到这一点? 自注册以来,我第一次在 mongoDB 中以哈希和盐形式保存密码。 我现在该如何更新?

这是我尝试用于更改密码的代码:

    router.get('/api/changePassword', function(req,res,next){
      req.checkBody('oldPass', 'Empty Password').notEmpty();
      req.checkBody('newPass', 'Password do not match').equals(req.body.confirmPassword).notEmpty();
    
      var user = new User();
      user.setPassword(req.body.newPass);
  user.save(function (err,data) {
        if (err) {
          res.render('register', {errorMessages: err});
        } else {
console.log("password set successfully");

    }
})
})

但在这里我怀疑它是否会更新到现有用户的数据库中,因为我在这里再次创建对象用户并保存它。它会在 Collections 中再次创建一个新用户并为此更新哈希和盐值吗?然后如何更新现有的用户密码哈希和盐值?

以下是哈希和盐模型用户架构代码:

userSchema.methods.setPassword = function(password) {
  this.salt = crypto.randomBytes(16).toString('hex');
  this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, 'sha1').toString('hex');
};

userSchema.methods.validPassword = function(password) {
  var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, 'sha1').toString('hex');
  return this.hash === hash;
};

module.exports = mongoose.model('User', userSchema); 

这是注册页面的路由代码,用户第一次注册并输入密码时:

router.route('/register')
  .get(function(req, res, next) {
    res.render('register', { title: 'Register a new account'});
  })
  .post(function(req, res, next) {
    req.checkBody('name', 'Empty Name').notEmpty();
    req.checkBody('email', 'Invalid Email').isEmail();
    req.checkBody('location', 'Empty Location').notEmpty();
    req.checkBody('password', 'Empty Password').notEmpty();
    req.checkBody('password', 'Password do not match').equals(req.body.confirmPassword).notEmpty();

    var errors = req.validationErrors();
    if (errors) {
      res.render('register', {
        name: req.body.name,
        email: req.body.email,
        location:req.body.location,
        errorMessages: errors
      });
    } else {
      var user = new User();
      user.name = req.body.name;
      user.email = req.body.email;
      user.location = req.body.location;
      user.setPassword(req.body.password);

      user.save(function (err,data) {
        if (err) {
          res.render('register', {errorMessages: err});
        } else {
console.log("user saved successfully");

}
})

【问题讨论】:

  • 我想知道这里有什么问题,运行setPassword() 方法后salt 和hash 属性是否保持不变?此外,更改密码时,您可能只想为用户创建一个.find(),而不是创建一个新密码。
  • 好的。但是在找到用户之后如何更新密码呢?我应该使用 .save()。如果是,那么如何?由于它在数据库中存储 has 和 salt 值而不是实际密码。所以我无论如何都必须运行 setPassword 命令! @gegs921
  • 我的第二个疑问是:我需要在setPassword之后保存密码吗?还是仅使用此命令更新盐值和哈希值? @gegs921
  • 我认为你应该做的是将setPassword重命名为createPassword,然后添加另一个以密码验证和createdPassword为参数的方法(这个新方法称为setPassword)。在这个新方法中,您将使用 .find() 方法根据 id 或用户名查找当前用户,然后使用 .update() 方法将此用户的数据更新为新密码,如果所有先前的检查都通过了。您可以在 mongoose 文档中找到有关 .find().update() 方法的说明。
  • 这样你只是在更新数据而不是创建新用户。

标签: node.js mongodb handlebars.js password-hash change-password


【解决方案1】:

对不起,我之前的解释是不正确的,所以我要写这个作为答案。您实际上无法单独在该方法中访问 User 对象,因此您必须执行以下操作:

使用User.find() 创建一个返回用户的函数。

然后将这个用户作为参数传递给我在上面评论中描述的你创建的新方法,并继续将.update()这个用户作为参数传递给setPassword()方法。

所以你的架构方法应该是:

createPassword()

validPassword()

setPassword()

您的代码中实际拥有使用这些方法设置此数据的函数的部分应类似于:

function findUser() {
  return User;
}

function setPassword(password) {
  User.setPassword(User, password, passwordValid /*This is a boolean*/)
}

【讨论】:

  • 什么都不懂@gegs921。
猜你喜欢
  • 2021-01-13
  • 1970-01-01
  • 2014-08-05
  • 2011-08-14
  • 2011-01-09
  • 2014-06-09
相关资源
最近更新 更多