Is it possible to pass cookie headers from API calls to the browser?
Unanswered
Northeast Congo Lion posted this in #help-forum
Northeast Congo LionOP
Is it possible - when using the App router Next.js - to pass Cookie headers that are received from API calls on to the browser? And if so, what is the way for it?
EDIT: The api call is done from within a page (RSC) and not a route handler
EDIT: The api call is done from within a page (RSC) and not a route handler
12 Replies
Toyger
export async function POST(request: NextRequest) {
// ...your post request logic here
// Set json response first
const response = NextResponse.json(
{
...userWithoutPassword,
},
{ status: 200 }
)
// Then set a cookie
response.cookies.set({
name: 'jwt',
value: token,
httpOnly: true,
maxAge: 60 * 60,
})
return response
}Northeast Congo LionOP
@Toyger Thanks for your reaction. Unfortunately that is not what we are looking for. The api call is done from within a page (RSC) and not a route handler
@Northeast Congo Lion <@536484914221285376> Thanks for your reaction. Unfortunately that is not what we are looking for. The api call is done from within a page (RSC) and not a route handler
it is not possible to set cookie on page response currently
@Ray it is not possible to set cookie on page response currently
Northeast Congo LionOP
Thanks @Ray . Do you know if there is a workaround for this?
can you do it with middleware?
@Ray can you do it with middleware?
Northeast Congo LionOP
Not sure how to do that. When rendering the page, we execute certain API calls to another server. Each of those calls might send Set-Cookie headers in reply. And we need to pass those on to the browser.
or use server action with useEffect
@Ray or use server action with useEffect
Northeast Congo LionOP
Do you mean we shouldn't execute the API call on SSR, but delay it until the browser runs the useEffect into a Server Action?
This wouldn't be desirable, since it negatively affects any page speed and might cause CLS.
This wouldn't be desirable, since it negatively affects any page speed and might cause CLS.
I mean use server action with useEffect for setting the cookie
not rendering the page
Northeast Congo LionOP
Thanks! We are going to try to make this work 🙂