Friday, 31 August 2018

Inserting a hashed password into MongoDB with bcrypt does not work, but plain text password inserts fine

Sorry if this is a really novice question. I'm currently learning Vue with a Node backend and am creating a Sign Up form.My form posts the sign up data to the backend using the fetch API. I catch the POST request in my server.js file and create a newUsermodel. When populating the password of the model, I run it through an encryption method using bcrypt:let newUser = new userModel({ username: username, email: email, password: hashPassword(password) }); hashPassword function:function hashPassword(password) { bcrypt.genSalt(saltRounds, (err, salt) => { if(err) { return console.log(err); } bcrypt.hash(password, salt, (err, hash) => { if(err) { return console.log(err); } return hash; }) }); save new user:newUser.save((err, user) => { if(err) { return res.send({ success: false, message: 'Server error: ' + err }); } return res.send({ success: true, message: username + ' successfully added.' }); }); and for some reason, the password will not save into my MongoDB instance hashed, but saves fine as plain text.Can anyone help please? Or point out a stupid mistake I've made somewhere.If you need any more info or code samples, please let me know.Thanks for your help. :)

Submitted August 31, 2018 at 08:05PM by _fka_

No comments:

Post a Comment