#How do I get client cookies in API route or Middleware?

17 messages ยท Page 1 of 1 (latest)

void spear
#

Basically I want to update expiration date so I need to get session token from cookies. Works on server actions but I can't get value in middleware or API routes even if I call server action inside.

tawny flintBOT
#

๐Ÿ”Ž 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)

rapid anchor
void spear
#

14.1.3

#

also tried setting as httpOnly cookie

  cookies().set("session", session.token, {
    expires: expirationDate,
    httpOnly: true,
  });
rapid anchor
void spear
#

upgraded still same happened but found out that i get cookies in middleware but not in api route ๐Ÿ˜•

not sure if upgrading had effect on middleware getting cookies

  await fetch("http://localhost:3000/api/update-session");
  console.log(request.cookies.getAll());

api route

export const dynamic = "force-dynamic";
export async function GET(request: NextRequest) {
  const sessionToken = cookies().get("session")?.value;
  const sessionToken2 = request.cookies.get("session")?.value;
  console.log({ sessionToken });
  console.log({ sessionToken2 });

  const sessionTokenFromServerAction = await getToken();
  console.log({ sessionTokenFromServerAction });
rapid anchor
#

rename middleware.ts to _middleware.ts

void spear
#

same thing happening

#

As a workaround I could do post method or call server action in a middleware ๐Ÿ˜„

rapid anchor
void spear
#

works in console ๐Ÿ‘๐Ÿพ

rapid anchor
#

if you make the request on the page, the request is made from server which doesn't have the cookies information

void spear
rapid anchor