I was using jwt authentication in my MERN app. I've got a quite simple login API which uses res.cookie to set the jwt token.router.post('/v1/login', passport.authenticate('local', { session : false}),(req,res)=>{ if(req.isAuthenticated()){ const {_id, username, role} = req.user; const token = signToken(_id); res.cookie('access_token', token, {httpOnly: true, sameSite: true}); res.status(200).json({isAuthenticated : true, user : { username, role}}); } Recently I'm converting my client-side from simple react to nextjs. And now res.cookie is failing to set the jwt as cookie.I came up with a solution though. I sent the token as json data and then in nextjs used 'js-cookie' to set it as cookie. But I'm not finding a way to set it as httpOnly.How can I handle this situation in a better way?
Submitted September 18, 2020 at 09:20PM by shamimbhai
No comments:
Post a Comment