Next.js Discord

Discord Forum

How to "forward" cookies from external auth?

Unanswered
Yellow Warbler posted this in #help-forum
Open in Discord
Yellow WarblerOP
I have an external api that is setting httpOnly cookies to the response :
//example
res.cookie('at', response.accessToken, {
      expires: new Date(Date.now() + 30 * 60000),
      httpOnly: true,
      path: '/',
      sameSite: 'strict',
      secure: false,
    });


On next.js, I fetch and have a response:
async function login() {
  const res = await fetch(
    `${process.env.NEXT_PUBLIC_API_URL_LOCAL as string}/auth/login`,
    {
      method: "POST",
      credentials: "include",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        email,
        password
      }),
    }
  );

  if (!res.ok) {
    return null;
  }

  return await res.json()
}

//console.log(res.headers)
[Symbol(headers map)]: Map(10) {
    'x-powered-by' => { name: 'X-Powered-By', value: 'Express' },
    'vary' => { name: 'Vary', value: 'Origin' },
    'access-control-allow-credentials' => { name: 'Access-Control-Allow-Credentials', value: 'true' },
    'set-cookie' => {
      name: 'Set-Cookie',
      value: 'at=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiNDc3NDhkMS0yODcxLTQyY2QtYjQwZC01NzIxOTEzMTQ0YmQiLCJ1c2VybmFtZSI6ImNhbGViIiwiaWF0IjoxNjg5ODkwOTY0LCJleHAiOjE2ODk4OTI3NjR9.PhH6mPeZBIaJSFqbAfS15HPShleJKOFXKeKXD9XGQDA; Path=/; Expires=Thu, 20 Jul 2023 22:39:24 GMT; HttpOnly; SameSite=Strict, rt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiNDc3NDhkMS0yODcxLTQyY2QtYjQwZC01NzIxOTEzMTQ0YmQiLCJ1c2VybmFtZSI6ImNhbGViIiwiaWF0IjoxNjg5ODkwOTY0LCJleHAiOjE2OTI0ODI5NjR9.7AUTCsN-qxFSK9IiLFCxyagquqEjM3MBEJIvXbyInBo; Path=/; Expires=Sat, 19 Aug 2023 22:09:24 GMT; HttpOnly; SameSite=Strict'
    },


The issue is, even though my next.js client is recieving the set-cookie header, it is not setting the cookies as intended. I assumed that this was due to the fact that the response is sent to the next.js server (aka not the browser). Is there a way to "forward" these cookies?

1 Reply

Wetterhoun
I’m interested in this thread as well. I am facing the same issue.