Node.js 如何在节点js中传递object中的变量值
我想将加密密码传递到对象中。所以当API调用时,密码将以加密的形式出现Node.js 如何在节点js中传递object中的变量值,node.js,rest,Node.js,Rest,我想将加密密码传递到对象中。所以当API调用时,密码将以加密的形式出现 app.post('/api/login', (req, res) => { const user = { id: 1, username: "Vinit", password: "12345" //Here I want to pass the encrypted password } jwt.sign(
app.post('/api/login', (req, res) => {
const user = {
id: 1,
username: "Vinit",
password: "12345" //Here I want to pass the encrypted password
}
jwt.sign({user: user}, 'secretkey', (err, token) => {
res.json({
token: token
});
});
});
如果尚未实现,则需要使用第三方库,如。然后你可以像这样生成散列密码
const bcrypt = require('bcryptjs');
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash("NON_HASHED_PASSWORD", salt, function(err, hash) {
console.log(hash) //<- Hashed password
});
});
const bcrypt=require('bcryptjs');
bcrypt.genSalt(10,函数(错误,盐){
哈希(“非哈希密码”,salt,函数(err,hash){
console.log(hash)/试试-它有很多策略,包括jwt
,google auth
,facebook
。等等。你的意思是像传递req.body.password
?是的@David,但格式是加密的。请用bcrypt libary散列它。和是不同的东西。