[英]How To Set Cookies for JWT in Node.js
I am trying to simply set the cookie
for the JSON Web Token
during authentication in my Node.js
and Express
web application. 我正在尝试在
Node.js
和Express
Web应用程序中的身份验证期间为JSON Web Token
简单地设置cookie
。
Since I cannot do res.redirect
after using res.cookie()
, how can I achieve this? 由于使用
res.cookie()
后无法执行res.redirect
,如何实现呢?
router.post('/', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (!user) { return res.redirect('/auth'); }
var theToken = jwt.encode({ id : user.id }, req.app.get('appSecret'));
res.cookie(theToken);
return res.redirect('/profile/' + username);
})(req, res, next);
});
Am I approaching this problem correctly? 我是否正确解决了这个问题? I cannot find much of anything on how people are setting their
JSON Web Token
s once they have created them. 一旦人们创建了
JSON Web Token
我就什么都找不到。
It seems that res.redirect
works with setting the cookies, despite what some other SO answers state. 似乎
res.redirect
可以用于设置cookie,尽管其他一些SO回答指出。 Hopefully this helps someone else. 希望这可以帮助其他人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.