简体   繁体   English

如何在Node.js中为JWT设置Cookie

[英]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.jsExpress 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM