【发布时间】:2018-09-10 17:58:16
【问题描述】:
我是 javascript 生态系统的新手,想添加 jwt 令牌以响应此注册路由器:
router.post('/register', (req, res)=> {
User.findOne({email: req.body.email})
.then(user => {
if(user) {
return res.status(400).json({error: 'Email already exists'});
} else {
const newUser = new User({
username: req.body.username,
email: req.body.email,
password: req.body.password
});
bcrypt.genSalt(10, (err, salt)=> {
bcrypt.hash(newUser.password, salt, (err, hash)=> {
if (err) throw err;
newUser.password = hash;
newUser.save()
.then(user => res.status(200).json(user)) //<=Problem is here
.catch(err => console.log(err));
} )
})
}
})
});
jwt sn-p(在longin 路由器上运行良好)是这样的:
const payload = {
username: user.username
}
//sign token
jwt.sign(
payload,
keys.secretOrKey,
{ expiresIn: 3600},
(err, token)=> {
res.json({
success: true,
token: 'Bearer '+ token,
username: username
});
});
问题是我不知道如何将 sn-p 添加到响应标头中。
当我在.then(user => 之后添加它时,我收到SyntaxError: Unexpected token const 错误。
我怎样才能做到?
【问题讨论】:
-
您是否在
.then(user =>后面添加了jwt sn-p 并使用花括号包裹了sn-p? -
是的,我刚刚处理了你在
.then(user =>之后看到的截图 -
@Momin 我的问题是如何将签名的 jwt 令牌集成到现有的承诺中。您提到的链接对此没有帮助。
标签: javascript node.js jwt