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.
#How do I get client cookies in API route or Middleware?
17 messages ยท Page 1 of 1 (latest)
๐ 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)
what version of next are you using?
14.1.3
also tried setting as httpOnly cookie
cookies().set("session", session.token, {
expires: expirationDate,
httpOnly: true,
});
I have no problem to get client cookie with 14.1.4, maybe you could try that?
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 });
could you try without the middleware enabled?
rename middleware.ts to _middleware.ts
same thing happening
As a workaround I could do post method or call server action in a middleware ๐
ah you are fetching the api on the page,
try go to http://localhost:3000/api/update-session on your browser
works in console ๐๐พ
if you make the request on the page, the request is made from server which doesn't have the cookies information
What about middleware? should I go for workaround?
I think its better to do that in middleware