0
votes

Well, i have simple user edit in node,express,mongodb. But i am unable to hash password to bcrypt. In registration works everything allright but that was tutorial...

Here is part of my routes/users.js Everything is updated but password is not hashed and i dont know what to do.

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

Here is my hasing for registration in models/users.js that i was inspired by.

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

I will be thankfull for any help.

1

1 Answers

0
votes

Well after hous its solved.

I just changed it to.

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