小编Ank*_*ari的帖子

httpOnly cookie 未随 getInitialProps [Next.js] 的请求一起发送

我正在从服务器设置一个httpOnlycookie (jwtToken),它正在前端设置。我可以在 devTools 中看到 cookie。

现在,如果我使用以下代码从 getInitalProps 向服务器发出请求,

  const data = await fetch(`${process.env.API_URL}/me`, {
    credentials: 'include',
  }).then((res) => res.json());
Run Code Online (Sandbox Code Playgroud)

cookiejwtToken不随请求一起发送。

我也尝试这样做

  const data = await fetch(`${process.env.API_URL}/me`, {
    credentials: 'include',
    headers: {
      cookie:  context.req.headers.cookie
    },
  }).then((res) => res.json());
Run Code Online (Sandbox Code Playgroud)

但这并没有那么有效。

但是,当我提出相同的请求时,useEffect 它工作得很好。任何人都可以帮我将其发送jwtToken到服务器getInitialProps吗?

express reactjs next.js

1
推荐指数
1
解决办法
4948
查看次数

标签 统计

express ×1

next.js ×1

reactjs ×1