Response Header Set Cookie from NextJS cookie not being attached in browser
Unanswered
Cape horse mackerel posted this in #help-forum
Cape horse mackerelOP
#vercel-help #help-forum
Hello Kind folks,
I am attaching cookies to my request header to be attached to my client machine post login but it is not getting attached upon successful login. Please help. Due to the cookies not being attached, the client side redirection is not happening. This is the code where I am setting the header
const cookie1 = serialize('token', String(data.token), {
httpOnly: true,
maxAge: 10800000, // 1 week
sameSite: 'Strict',
secure: true, // Ensure the cookie is sent only over HTTPS
path: '/'
});
const cookie2 = serialize('classRole', String(data.roles[0]), {
httpOnly: true,
maxAge: 10800000, // 1 week
sameSite: 'Strict',
secure: true, // Ensure the cookie is sent only over HTTPS
path: '/'
})
const cookie3 = serialize('classId',String(data.map.id), {
httpOnly: true,
maxAge: 10800000, // 1 week
sameSite: 'Strict',
secure: true, // Ensure the cookie is sent only over HTTPS
path: '/'
})
let cookieArr = [cookie1,cookie2,cookie3,cookie4,cookie5];
res.setHeader(
'Set-Cookie',
cookieArr,
)
res.status(200);
Hello Kind folks,
I am attaching cookies to my request header to be attached to my client machine post login but it is not getting attached upon successful login. Please help. Due to the cookies not being attached, the client side redirection is not happening. This is the code where I am setting the header
const cookie1 = serialize('token', String(data.token), {
httpOnly: true,
maxAge: 10800000, // 1 week
sameSite: 'Strict',
secure: true, // Ensure the cookie is sent only over HTTPS
path: '/'
});
const cookie2 = serialize('classRole', String(data.roles[0]), {
httpOnly: true,
maxAge: 10800000, // 1 week
sameSite: 'Strict',
secure: true, // Ensure the cookie is sent only over HTTPS
path: '/'
})
const cookie3 = serialize('classId',String(data.map.id), {
httpOnly: true,
maxAge: 10800000, // 1 week
sameSite: 'Strict',
secure: true, // Ensure the cookie is sent only over HTTPS
path: '/'
})
let cookieArr = [cookie1,cookie2,cookie3,cookie4,cookie5];
res.setHeader(
'Set-Cookie',
cookieArr,
)
res.status(200);
2 Replies
Cape horse mackerelOP
This is my next.config.js file
/ @type {import('next').NextConfig} */
/ @type {import('next').NextConfig} /
const nextConfig = {
async headers() {
return [
{
// Routes this applies to
source: "/api/(.)",
// Headers
headers: [
// Allow for specific domains to have access or * for all
{
key: "Access-Control-Allow-Origin",
value: "*",
},
// Allows for specific methods accepted
{
key: "Access-Control-Allow-Methods",
value: "GET, POST, PUT, DELETE, OPTIONS",
},
// Allows for specific headers accepted (These are a few standard ones)
{
key: "Access-Control-Allow-Headers",
value: "Content-Type, Authorization",
}
],
},
];
},
};
module.exports = nextConfig
/ @type {import('next').NextConfig} */
/ @type {import('next').NextConfig} /
const nextConfig = {
async headers() {
return [
{
// Routes this applies to
source: "/api/(.)",
// Headers
headers: [
// Allow for specific domains to have access or * for all
{
key: "Access-Control-Allow-Origin",
value: "*",
},
// Allows for specific methods accepted
{
key: "Access-Control-Allow-Methods",
value: "GET, POST, PUT, DELETE, OPTIONS",
},
// Allows for specific headers accepted (These are a few standard ones)
{
key: "Access-Control-Allow-Headers",
value: "Content-Type, Authorization",
}
],
},
];
},
};
module.exports = nextConfig
Cape horse mackerelOP
Hey guys,
can anyone help here ? I am struggling with this since yesterday
can anyone help here ? I am struggling with this since yesterday