Cookie Error #191600
-
🏷️ Discussion TypeQuestion BodyThe cookie is available in the backend as httpOnly, but when I try to access it in Next.js middleware, the token is undefined. Here’s how I set the cookie in the backend:
In my Next.js middleware, I try to access it like this: const token = req.cookies.get("token")?.value; But it returns undefined. Why is the cookie not accessible in middleware even though it exists in the backend? What could be causing this issue? Guidelines
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
This usually happens due to how cookies behave between the backend and Next.js middleware, not because your code is wrong. Common causes:
Fix: fetch("http://localhost:5000/api/...", { Also enable CORS on your backend:
Fix:
Fix:
They are treated as different origins. Fix:
If the cookie is not included in the request, middleware cannot read it. Summary:
Recommended fix:
|
Beta Was this translation helpful? Give feedback.
This usually happens due to how cookies behave between the backend and Next.js middleware, not because your code is wrong.
Common causes:
If your backend and frontend are on different domains or ports, the browser will NOT send cookies by default.
Fix:
Make sure your request includes credentials:
fetch("http://localhost:5000/api/...", {
credentials: "include"
});
Also enable CORS on your backend:
You are using sameSite: "strict", which blocks cookies on cross-site requests.
Fix:
Use:
sameSite: "lax"
OR
sameSite: "none" (requires secure: true)
If secure is true, c…