Creating cookie in API route handler #76748
-
I use the app router, and I call API routes with built-in fetch (including credentials). I want to create a cookie for access token in authentication logic. The problem is the cookie isn't created in the POST handler (I use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Did you specify a I'd like to see how you set cookie, some pseudo-code, and also, in the devTools, of your browser, when the POST request arrives, you should be able to see the |
Beta Was this translation helpful? Give feedback.
Right, here you have two different HTTP requests. Within your server action, you have scope to set the cookie back to the browser, however, you "fork", a new HTTP request, to your API route/Router handler, where you set cookies, but that'd be setting the cookies on the request that was forked, not the request that started from the browser.
You are just collecting the
json
body payload, of the forked request, dropping theSet-Cookie
header it might have had, and terminating the request. You'd need to collect theSet-Cookie
from the forked response, and the append that to what you send down... or, could you... just do this?