admin管理员组文章数量:1122832
I am trying to set the cookies in the browser Application. I get the cookies values from the response of the API's headers as set-cookie. This is the value:
'set-cookie': 'sessionid=<REDACTED>; expires=Sun, 22 Nov 2025 04:02:33 GMT; HttpOnly; Max-Age=31536000; Path=/, expires=Sat, 04 Nov 2024 04:02:33 GMT; HttpOnly; Max-Age=5453453; Path=/',
Here is the page calling the API on the server side.
import { getMainData } from "@/api/server";
export default async function Home() {
const response = await getMainData();
return (
<div>
Home Data
</div>
);
}
This is the API function.
import { BASE_URL } from "@/lib/constants";
export async function getMainData() {
try {
const _res = await fetch(`${BASE_URL}/main/data/`, {
method: "GET",
credentials: "include",
mode: "cors",
});
console.log("_res", _res);
const _data = await _res.json();
return {
response: _data,
};
} catch (error) {
return {
response: error,
};
}
}
Kindly give the solution to set the cookies in the browser.
I am trying to set the cookies in the browser Application. I get the cookies values from the response of the API's headers as set-cookie. This is the value:
'set-cookie': 'sessionid=<REDACTED>; expires=Sun, 22 Nov 2025 04:02:33 GMT; HttpOnly; Max-Age=31536000; Path=/, expires=Sat, 04 Nov 2024 04:02:33 GMT; HttpOnly; Max-Age=5453453; Path=/',
Here is the page calling the API on the server side.
import { getMainData } from "@/api/server";
export default async function Home() {
const response = await getMainData();
return (
<div>
Home Data
</div>
);
}
This is the API function.
import { BASE_URL } from "@/lib/constants";
export async function getMainData() {
try {
const _res = await fetch(`${BASE_URL}/main/data/`, {
method: "GET",
credentials: "include",
mode: "cors",
});
console.log("_res", _res);
const _data = await _res.json();
return {
response: _data,
};
} catch (error) {
return {
response: error,
};
}
}
Kindly give the solution to set the cookies in the browser.
Share Improve this question edited Nov 25, 2024 at 7:45 eternal_white 4804 silver badges14 bronze badges asked Nov 23, 2024 at 4:10 JohnJohn 3231 gold badge3 silver badges10 bronze badges1 Answer
Reset to default 0you can import cookie like this
import { cookies } from "next/headers";
and after that you should use set method from cookie which you have imported from next/headers like this :
cookies().set('name of token', yourtoken , { httpOnly: true })
and then return your token
版权声明:本文标题:javascript - Set Cookie of API Headers Response in browser cookies in App router in nextjs 14 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736299867a1930611.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论