简体   繁体   中英

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.

Since I cannot do res.redirect after using res.cookie() , how can I achieve this?

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.

It seems that res.redirect works with setting the cookies, despite what some other SO answers state. Hopefully this helps someone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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