Next.js Discord

Discord Forum

Set cookie from incoming response

Unanswered
Youssef posted this in #help-forum
Open in Discord
I'm fetching my own server in a server component
and the response includes some cookies that I want to store
the problem lies in that I don't know how to parse it since it comes from the headers
for example:
export default async function Home() {
    const response = await fetch(
        "my backend server",
        {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify("credentials"),
            credentials: "include",
        }
    );

    // How to store cookies coming from the response headers?
    response.headers.getSetCookie(); // ["session=token Path=/; Expires=Date; HttpOnly..."]
    
    return <p>Some UI</p>;
}

9 Replies

I think you have to use middlewire to set cookies
@riský I think you have to use middlewire to set cookies
How?
Is there a library doing that or something?
Ok I got you
I'll try it
But wait how will I use middleware and I make the request in a server component?
You would have to do the request in the middlewire
@riský You would have to do the request in the middlewire
Is there an easier approach?
I can't use middleware because the request depends on credentials the user sends
1. why can't the user send the request?
2. can't middlewire intercept the request (and get the creds)?