I was doing an axios call to a certain json file. Basically I am trying to get an input from a front end framework and passing the data to my server via express.
So what am I trying to do here is that I want to encrypt first the password that I got from request.body.password
before saving it to the database.
The default documentation from bcrypt, is an asynchronous call using callbacks so what I did is I console log the result before and after the encryption but it seems that bycrypt only works inside itself and doesn't encrypt the password before saving it to the database.
router.post('/call/user', (req, res) => {
var user = new User(req.body);
axios
.get(
`http://localhost:3000/mydata/data.json`
)
.then(response => {
user.lat = response.data.results[0].geometry.location.lat;
user.lng = response.data.results[0].geometry.location.lng;
console.log('here is the user password... >>>>>>>>>', req.body.password) // returns original text password
bcrypt.hash(req.body.password, 10, function(err, hash){
if(err){
console.log(err);
}
user.password = hash;
});
console.log('this is the hash password >>>>>>>>>', user.password)
// i expect this to return the encrypted password but it seems it doesn't encrypt it outside the callback
// saving data to user
user.save(err => {
if (err) {
console.log('problem adding user', err);
} else {
res.status(200).send();
}
});
})
.catch(err => {
console.log('err on user', err);
res.status(500).send();
});
});
In the end, if I console.log this it will show me the original text password without encryption on it save to the db.
Any idea what am I missing here? How can fixed my function to do the things I want it to do?
Have you tried using the technique 1?
bcrypt.genSalt(saltRounds, function(err, salt) {
bcrypt.hash(myPlaintextPassword, salt, function(err, hash) {
// Store hash in your password DB.
});
});
And then just return the user.password?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.