cookies - nextjs cookie 未添加到 axios 请求客户端
问题描述
我有一个在 UI 端使用 nextjs 并在 API 上使用 nestjs 的应用程序。
在 nestjs 中,我使用基于护照 cookie 的身份验证。
我正在使用 authCookieHeader 设置请求标头:
export const authCookieHeader = (session) => {
const { accessToken, refreshToken } = session;
const cookieVal = `${accessToken}; ${refreshToken};`;
return {
headers: {
Cookie: cookieVal,
withCredentials: true,
},
};
};
在服务器端 props 中进行调用时,cookie 被添加到来自 nextjs 的 axios 请求中。
const config = authCookieHeader(session);
let profile = {};
if (session) {
try {
const resp = await axios.get(
process.env.SH_API_BASEURL + `/users/profile`,
config
);
....
}
但在客户端,cookie 未添加到 axios 请求中,我收到“未登录”错误
const config = authCookieHeader(session);
console.log('config in client');
console.log(config);
const url = process.env.NEXT_PUBLIC_SH_API_BASEURL + '/users/profile';
console.log(url);
const { data: test1 } = await axios.get(url, config);
console.log(test1);
在客户端中,axios 配置是
headers: { Cookie: "Authentication=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEzNTQ1N2FiLWI4MGUtNDU2OC1hY2RiLWNiODZmYTJlNGQxMyIsImlhdCI6MTYyOTAxNTIxNiwiZXhwIjoxNjI5OTE1MjE2f" }
解决方案
推荐阅读
- jhipster - JHipster 使用哪些底层机制进行代码生成
- python - 如何在本网站上刮取股息收益率
- python - AWS Textract 表提取将其中包含逗号的整数的行分解为另一列
- c# - ASP.Net Cascading 下拉菜单在第一个下拉字段中具有重复值
- php - 通过 GoDaddy 托管 (PHP) 发送电子邮件的正确/推荐方式
- r - 将 VCF 文件的 INFO 解析为 R 数据帧
- python - Pandas 列值比较和过滤其他列 Series 的真值不明确 Error: a.any() or a.all()
- html - 在 HTML 中使星期六和星期日不可选择
- jflex - JFlex 参数化令牌
- pic - 将 PIC24E 与 ICD3 连接