I haven't work with cookies before and currently trying to store data in cookies.This is the response I am getting in console.log()
I want to set cookies in header with setHeader(). This is the async function getjwt() using to send data to backend.
async function getjwt(req, res) {
const apiRes= await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/auth/jwt/create`,
{
method: "POST",
headers: {
Accept: "application/json",
"content-Type": "application/json",
},
body: Body,
}
);
console.log(apiRes);
const data = await apiRes.json();
console.log("+++++++++++++++++++++");
console.log(data.access);
console.log("+++++++++++++++++++++");
if (apiRes.status === 200) {
res.setHeader("Set-Cookie", [
cookie.serialize("access", data.access, {
httpOnly: true,
secure: false,
maxAge: 60 * 30,
sameSite: "strict",
path: "/auth/",
}),
cookie.serialize("refresh", data.refresh, {
httpOnly: true,
secure: false,
maxAge: 60 * 60 * 24,
sameSite: "strict",
path: "/auth/",
}),
]);
} else {
return res.status(apiRes.status).json({
error: "Authentication failed",
});
}
}
ERROR-
Uncaught (in promise) TypeError: res is undefined
how can I set Header in response.