设置标头响应
我以前从未使用过cookie,目前尝试将数据存储在cookie中。这是我在console.log()
我想与setheader在header中设置cookie ()。这是用于将数据发送到后端的异步函数getJwt()。
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",
});
}
}
错误 -
Uncaught (in promise) TypeError: res is undefined
如何将标题设置为响应。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论