Next.js Discord

Discord Forum

Cookies Missing in fetch response

Answered
Coton de Tulear posted this in #help-forum
Open in Discord
Coton de TulearOP
👋, i updated from 15.1.4 to 15.3.2 and my fetch response doesn't contain the cookies i set in the router handler anymore.

This is my old code
const response = await fetch("/api/jwt", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "X-User-Role": role,
    "X-Force-Role-Refresh": "true",
  },
});

const cookies = response.headers.get("x-middleware-set-cookie");


Route Handler:
const cookieData = {
  httpOnly: true,
  sameSite: "lax" as const,
  path: "/",
  maxAge: 24 * 60 * 60,
  secure: process.env.NODE_ENV === "production",
  domain: process.env.NODE_ENV === "production" ? "domain" : "localhost",
};

const nextResponse = NextResponse.json({ success: true });
nextResponse.cookies.set("jwt", token, cookieData);
return nextResponse;

The NextResponse docs show that you can set cookies with NextResponse.next() but that gives me a different error that i can't use it in Route Handlers.
Answered by Coton de Tulear
My bad, i refactored it to just use server actions instead of a route handler to manage cookies.
View full answer

5 Replies

Coton de TulearOP
Not sure what extension u are referring to, but i can see the cookie in the browser and also in a console.log on the nextresponse in the route handler. Its also in cookies under the network tab
browser cookie extension, which can be used to view cookies
@Coton de Tulear updates?
@Anay-208 | Ping in replies <@298041202723389441> updates?
Coton de TulearOP
My bad, i refactored it to just use server actions instead of a route handler to manage cookies.
Answer