#How to "forward" cookies from external auth?

3 messages · Page 1 of 1 (latest)

dapper scarab
#

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?

sharp treeBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

      🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in <id:customize>

      ✅ You can mark a message as the answer for your post with `Right click -> Apps -> Mark Solution`
      (if you don't see the option, try refreshing Discord with Ctrl + R)
tiny quartz
#

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