Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't set multiple Set-Cookie headers in an action #7334

Closed
1 task done
martinmckenna opened this issue Sep 4, 2023 · 4 comments
Closed
1 task done

Can't set multiple Set-Cookie headers in an action #7334

martinmckenna opened this issue Sep 4, 2023 · 4 comments

Comments

@martinmckenna
Copy link

martinmckenna commented Sep 4, 2023

What version of Remix are you using?

1.6.3

Are all your remix dependencies & dev-dependencies using the same version?

  • Yes

Steps to Reproduce

  1. Have your client reach out to a remix route that as an action like so:
import type { ActionArgs } from "@remix-run/node";
import { createCookie, json } from "@remix-run/node";

import { AUTH_COOKIE, AUTH_EXP_COOKIE } from "app/cookies"

export const setCookie = (key: string, value: any) => {
  const createdCookie = createCookie(key, {
    path: "/",
    sameSite: "lax",
  });
  return createdCookie.serialize(value);
};

export async function action({ request }: ActionArgs) {
  return json(
    {},
    {
      headers: {
        "Set-Cookie": await setCookie(AUTH_COOKIE, ""),
        // @ts-expect-error
        "Set-Cookie": await setCookie(AUTH_EXP_COOKIE, ""),
      },
    }
  );
}
  1. Call endpoint
  2. Check response headers in dev tools and see there is only 1 Set-Cookie header:

Screen Shot 2023-09-04 at 4 22 53 PM

Expected Behavior

See 2 Set-Cookie headers

Actual Behavior

Only seeing 1 Set-Cookie header

@martinmckenna
Copy link
Author

this was already brought up a while back after not working with redirect, but someone mentioned it wasn't working in loaders and now i'm seeing similar behavior in an action, so I figured i'd create a new issue:

#231 (comment)

@martinmckenna
Copy link
Author

nvm fix is to create a new Headers() instance and add them that way:

  const authCookieHeaders = new Headers();
  authCookieHeaders.append("Set-Cookie", await setCookie(AUTH_COOKIE, ""));
  authCookieHeaders.append("Set-Cookie", await setCookie(AUTH_EXP_COOKIE, ""));

  return json(
    {},
    {
      headers: authCookieHeaders,
    }
  );

@MattyBalaam
Copy link

nvm fix is to create a new Headers() instance and add them that way:

  const authCookieHeaders = new Headers();
  authCookieHeaders.append("Set-Cookie", await setCookie(AUTH_COOKIE, ""));
  authCookieHeaders.append("Set-Cookie", await setCookie(AUTH_EXP_COOKIE, ""));

  return json(
    {},
    {
      headers: authCookieHeaders,
    }
  );

This throws a type error in TypeScript

Screenshot 2023-09-28 at 13 47 35

@martinmckenna
Copy link
Author

@MattyBalaam can you post your full code. Also i'm on typescript 5.2.2 if that makes a difference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants