Unable to Set Cookies from Express.js to Next.js SSR Component (app router) #153971
Replies: 4 comments
-
|
Welcome to the GitHub Community, @giorgi225, we're happy you're here! You are more likely to get a useful response if you are posting your question(s) in the applicable category and are explicit about what your project entails--giving a few more details might help someone give you a nudge in the right direction. I've gone ahead and moved it for you. Good luck! |
Beta Was this translation helpful? Give feedback.
-
|
This happens because Next.js server components cannot access browser cookies directly. The cookies.get() method reads cookies from the incoming server request, not the browser. When your Express backend returns a Set-Cookie header, it works in the browser (client components) because the browser automatically handles it, but server-side fetches don’t automatically propagate it to the client. To handle this in SSR, you need to manually extract the Set-Cookie header from the Express response and then use cookies.set() in Next.js to set it for the outgoing response. This ensures the new cookie reaches the browser correctly. Here's my implementation for reference: https://github.com/raza-h/blog-fe/blob/master/lib/server-utils.ts |
Beta Was this translation helpful? Give feedback.
-
|
Hey! I see exactly what's happening here. The reason this works in the browser but not in SSR is because your getSession function is making a server-to-server request. When Express sends the Set-Cookie header during this fetch, it goes straight to your Next.js server instead of the user's browser. The browser completely misses it! Since your getSession function is a Server Action (indicated by "use server"), you can fix this by explicitly telling Next.js to set the cookie using the new token Express returns in its JSON response. Just update the success block in your getSession function to manually set the cookie like this: TypeScript } |
Beta Was this translation helpful? Give feedback.
-
|
From what I’ve run into, this usually happens because when a request is made from a Next.js server component, it’s basically a server-to-server call. The browser isn’t directly involved in that exchange, so even if Express sends a Set-Cookie header, it just reaches the Next.js server and stops there instead of being stored in the user’s browser. In client components it works because the browser automatically handles cookies, but with SSR you have to pass things along yourself. One approach that worked for me was to read the Set-Cookie (or the token in the response body) from the backend response and then explicitly set it using the cookies API in Next.js so it becomes part of the outgoing response to the browser. It feels a bit awkward the first time you deal with it, because you expect cookies to “just work,” but once you think of SSR as one server talking to another server, it makes more sense why the browser never sees the cookie unless you forward it manually. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Select Topic Area
General
Body
I have an Express.js backend running on http://localhost:8000 and a Next.js frontend (App Router) running on http://localhost:3000.
What Works: • When making a request from a Next.js client component (browser) to the Express backend, cookies are set successfully. • When checking the response headers in a server component (SSR) or API route, I can see the Set-Cookie header coming from Express.
What Works: • When making a request from a Next.js client component (browser) to the Express backend, cookies are set successfully. • When checking the response headers in a server component (SSR) or API route, I can see the Set-Cookie header coming from Express.
What I Tried: • Setting credentials: "include" in the fetch request. • Making a Next.js API route as a proxy and forwarding the Set-Cookie header. • Using different SameSite policies (Lax, Strict, None with Secure). • Ensuring CORS is properly configured on Express (Access-Control-Allow-Credentials: true). • Manually setting a cookie in the Next.js response (nextResponse.cookies.set("key", "value")).
This is my SSR function on NextJS to fetch request on ExpressJS API. To get info if user is authenticated or not I am sending access token. If access token is expired and refresh token is valid then I am refreshing the access token but new access token is not adding to browser cookies.
this is my expressjs refreshToken function where i setting new accessToken but it not working as i say above. it only works when request comes from nextjs client component but not when request comes from nextjs ssr
Beta Was this translation helpful? Give feedback.
All reactions