简体   繁体   English

当用户在nodejs中更改密码时如何更新mongoDB中的salt和hash值?

[英]How to update salt and hash value in mongoDB when user changes the password in nodejs?

I have to implement a functionality for the user who can after login change the password to new one.我必须为登录后可以将密码更改为新密码的用户实现一项功能。 For that I want to update the hash and salt value for new password set.为此,我想更新新密码集的hashsalt值。 How can I do that?我怎样才能做到这一点? Since on registration I am saving the password first time in hash and salt form in mongoDB.自从注册以来,我第一次在 mongoDB 中以哈希和盐的形式保存密码。 How can I update that now?我现在该如何更新?

Here is the code I am trying to use for password change:这是我尝试用于更改密码的代码:

    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");

    }
})
})

But here I doubt that it will get updated into the existing user's database since I am creating the object user again here and saving it.但在这里我怀疑它是否会更新到现有用户的数据库中,因为我在这里再次创建对象用户并保存它。 Will it create again a new user in Collections and update the hash and salt value for that?它会在 Collections 中再次创建一个新用户并为此更新哈希值和盐值吗? How to then update the existing user password hash and salt value?然后如何更新现有的用户密码哈希值和盐值?

Below is the hash and salt model User schema code:以下是哈希和盐模型用户架构代码:

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

And this is the route code of registartion page, first time when user registers and put password:这是注册页面的路由代码,用户第一次注册并输入密码时:

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");

}
})

Sorry, my previous explanation was incorrect, so I am going to write this as an answer.对不起,我之前的解释不正确,所以我打算把这个写下来作为答案。 You won't actually be able to access the User object in the method alone, so what you will have to do is something like the following:您实际上无法单独访问该方法中的 User 对象,因此您需要做的是如下所示:

create a function with User.find() that returns the user.使用User.find()创建一个返回用户的函数。

Then pass in this user as a parameter to the new method you created as I described in the comment above, and continue to .update() this user that was passed into the setPassword() method as a parameter.然后,通过在这个用户作为一个参数,因为我在上面的注释中描述您创建的新方法,并不断.update()此用户已传递到setPassword()方法作为参数。

So your methods for the schema should be:所以你的模式方法应该是:

createPassword()

validPassword()

setPassword()

and the part of your code where you actually have the function that uses these methods to set this data should look something like:并且您实际拥有使用这些方法设置此数据的函数的代码部分应如下所示:

function findUser() {
  return User;
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM