[英]How to call bcrypt inside axios call
我正在对某个json文件进行axios调用。 基本上,我试图从前端框架获取输入,并通过express将数据传递到我的服务器。
所以我在这里想要做的是我想先加密从request.body.password
获得的密码,然后再将其保存到数据库中。
bcrypt的默认文档是使用回调的异步调用,因此我所做的是我在加密之前和之后控制台记录结果,但似乎bycrypt仅在自身内部起作用,并且在将密码保存到数据库之前不会对其进行加密。
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();
});
});
最后,如果我使用console.log,它将显示原始密码,并且没有加密,并保存到数据库中。
知道我在这里想念什么吗? 如何固定我的功能来完成我想做的事情?
您是否尝试过使用技术1?
bcrypt.genSalt(saltRounds, function(err, salt) {
bcrypt.hash(myPlaintextPassword, salt, function(err, hash) {
// Store hash in your password DB.
});
});
然后只需返回user.password?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.