Node.js хэш обновил пароль - PullRequest
       31

Node.js хэш обновил пароль

0 голосов
/ 10 сентября 2018

Ну, у меня есть простое пользовательское редактирование в node, express, mongodb.Но я не могу хэшировать пароль для bcrypt.В регистрации работает все хорошо, но это было учебное пособие ...

Вот часть моих маршрутов / users.js Все обновляется, но пароль не хэшируется, и я не знаю, что делать.

router.post("/profile", function (req, res) {
  let user = {};

  user.firstname = req.body.firstname;
  user.lastname = req.body.lastname;
  user.email = req.body.email;
  user.password = req.body.password;
  user.password2 = req.body.password2;

  req.checkBody("firstname", "Firstname is required").notEmpty();
  req.checkBody("lastname", "Lastname is required").notEmpty();
  req.checkBody("email", "Email is required").notEmpty();
  req.checkBody("email", "Email is not valid").isEmail();
  req.checkBody("password", "Password is required").notEmpty();
  req
    .checkBody("password", "Password must be longer then 8 chars bitch")
    .len(8, 64);
  req
    .checkBody("password2", "Passwords do not match")
    .equals(req.body.password);

  var errors = req.validationErrors();

  if (errors) {
    res.render("profile", {
      errors: errors
    });

  } else {

  bcrypt.genSalt(10, function(err, salt) {
    bcrypt.hash(user.password, salt, function(err, hash) {
      user.password = hash;
    });
  });

  let query = {_id:req.user.id}

  User.update(query, user, function(err){
    if(err){
      console.log(err);
      return;
    } else {
      res.redirect('/');
    }
  });
}});

Вот мое желание зарегистрироваться в models / users.js, на которое меня вдохновили.

module.exports.createUser = function(newUser, callback) {
  bcrypt.genSalt(10, function(err, salt) {
    bcrypt.hash(newUser.password, salt, function(err, hash) {
      newUser.password = hash;
      newUser.save(callback);
    });
  });
};

Я буду благодарен за любую помощь.

1 Ответ

0 голосов
/ 10 сентября 2018

Хорошо, после того, как дом решен.

Я просто изменил его на.

  bcrypt.genSalt(10, function(err, salt) {
    bcrypt.hash(user.password, salt, function(err, hash) {
      user.password = hash;



  let query = {_id:req.user.id}

  User.update(query, user, function(err){
    if(err){
      console.log(err);
      return;
    } else {
      res.redirect('/');
    }
  });
});
  });
}});
...