I'm implementing authentication for a Next.js frontend with an Express.js backend, and handling cookies in a production environment presented several challenges. Cookies weren't accessible on the frontend, logout didn't clear cookies properly, and cookies were unavailable in Next.js middleware for authentication.
I'm implementing authentication for a Next.js frontend with an Express.js backend, and handling cookies in a production environment presented several challenges. Cookies weren't accessible on the frontend, logout didn't clear cookies properly, and cookies were unavailable in Next.js middleware for authentication.
Share asked Mar 7 at 10:47 Rao ImtinanRao Imtinan 901 silver badge6 bronze badges1 Answer
Reset to default 1Setting Cookies in Express.js Backend
When setting a cookie, you must ensure that the domain, security options, and SameSite settings match your frontend setup. If your frontend and backend are on different subdomains, you need to allow cross-site cookies.
const token ='klldfhjsdhfkjdshfkdsfjk'
const cookiesOpts = {
httpOnly: true,
secure: true,
sameSite: 'None',
maxAge: 24 * 60 * 60 * 1000, // Cookie expires in 1 day
domain: '.example', // Allows subdomain sharing (e.g., api.example & app.example)
path: '/',
};
res.cookie('token', token, cookiesOpts);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744935701a4602007.html
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744935701a4602007.html
评论列表(0条)