Next.js Discord

Discord Forum

Nextauth Cookies not getting read in server fetch

Answered
zhefciad posted this in #help-forum
Open in Discord
When I'm making a fetch in a server component with routes, cookies() returns an empty array. But on a client component, it returns the cookies. Idk if it's like a timing issue because the fetch is very early since it's in a server component.



export async function POST(request: NextRequest) {
  try {
    const { endpoint, options } = await request.json();
    console.log("Request endpoint:", endpoint);
    console.log("Request options:", options);

    const cookieStore = cookies();
    console.log("Cookie store:", cookieStore.getAll());
Answered by joulev
that's not the relevant part. this is the relevant part:

do not fetch your own API routes or route handlers (from now on, just "route handlers") in server components, instead just call the server-side logic directly
View full answer

5 Replies

This is the relevant part I saw:

The fetch does not have access to headers and cookies automatically. You need to manually extract the relevant informations with headers and cookies and pass them to the fetch call.


In my case, it already exists. I'm not capturing it from a response.
does this mean there's no way to achieve this?
@zhefciad This is the relevant part I saw: The fetch does not have access to headers and cookies automatically. You need to manually extract the relevant informations with headers and cookies and pass them to the fetch call. In my case, it already exists. I'm not capturing it from a response.
that's not the relevant part. this is the relevant part:

do not fetch your own API routes or route handlers (from now on, just "route handlers") in server components, instead just call the server-side logic directly
Answer
got it. Thanks!