I am developing a web app using Next.js and I used HTTP-only cookies to manage authentication. I set a cookie called token using the following code. I used an npm package called cookie for this
res.setHeader(
"Set-Cookie",
cookie.serialize("token", data.jwt, {
httpOnly: true,
maxAge: 60 * 60 * 24,
sameSite: "strict",
path: "/",
})
);
Now when I use console.log(req.headers.cookie);
it logs a cookie like _ga=GA1.1; token=jnsjndjsnjd
this. So my question is how can I get hold of the second cookie called token.
When I do console.log(req.headers.cookie.token)
it says undefined.
I was able to find the solution. We need to parse the cookie using cookie.parse
, so we can access the token cookie as req.headers.cookie.token
.