Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Cookies are missing on a fetch request in a Next.js app

Errorfetch with credentials: 'include' sends no Cookie header

Short answer

In the browser, cookies on a cross-origin fetch need credentials include plus an API that answers with an exact origin and Access-Control-Allow-Credentials true, and a cookie the browser allows cross-site. In a Server Component there is no browser, so you read the cookie and pass it yourself.

  • Applies to Next.js 15, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Decide where the fetch runs: in a Client Component (the browser sends cookies) or in a Server Component or Route Handler (no browser cookies). Then look at the response headers of the failing call.

    curl -i -H "Origin: https://app.example.com" https://api.example.com/me
  2. 2Match what you see, then jump to its fix

    1. 1The request goes to another origin (another domain or port) and no Cookie header leaves the browser.credentials is not set, so the default same-origin mode appliesFix
    2. 2Console shows a CORS error mentioning the wildcard origin or Access-Control-Allow-Credentials.The API does not opt in to credentialed CORSFix
    3. 3CORS headers are correct but the Cookie header is still empty on a cross-site call.The browser blocks the cookie as a third-party cookieFix
    4. 4The call is in a Server Component or Route Handler, and the API sees no cookie at all.fetch runs on the Next.js server, not in the browserFix

What does the error look like?

  • fetch with credentials: 'include' sends no Cookie header
  • Access-Control-Allow-Credentials

Cause 1 of 4

credentials is not set, so the default same-origin mode applies

1Check

fetch defaults to credentials same-origin, which sends cookies only to the same origin.

grep -rn "fetch(" --include="*.ts" --include="*.tsx" src | head

2Fix

Add credentials include to the cross-origin call.

const res = await fetch("https://api.example.com/me", {
  credentials: "include",
});

3Verify

In the browser network tab, the request shows a Cookie header.

# DevTools > Network > the request > Request Headers

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 4

The API does not opt in to credentialed CORS

1Check

The browser rejects a credentialed response unless it has Access-Control-Allow-Credentials true and an explicit Access-Control-Allow-Origin, never *. Preflight responses need the credentials header too.

curl -i -X OPTIONS -H "Origin: https://app.example.com" -H "Access-Control-Request-Method: GET" https://api.example.com/me

2Fix

On the API, answer with the exact origin, credentials true and Vary Origin.

Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Credentials: true
Vary: Origin

3Verify

Repeat the curl check; the three headers appear, and the browser stops reporting a CORS error.

curl -i -H "Origin: https://app.example.com" https://api.example.com/me

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 4

The browser blocks the cookie as a third-party cookie

1Check

Third-party cookie policies and the cookie's SameSite attribute can stop it being sent even when the server allows credentials. Look at the Set-Cookie header your API sends.

curl -s -D - -o /dev/null https://api.example.com/login | grep -i set-cookie

2Fix

Set the SameSite attribute to suit your setup, and where you can, serve the API from the same site as the app so the cookie is first-party.

Set-Cookie: session=abc; Secure; HttpOnly; SameSite=Lax

3Verify

Reload the app and check the Cookie header in the network tab.

# DevTools > Application > Cookies

Confirmed by developer.mozilla.org

Did this fix it?

Cause 4 of 4

fetch runs on the Next.js server, not in the browser

1Check

Server-side fetch has no browser cookie jar. The incoming request cookies are readable with cookies() from next/headers, which is async in Next.js 15.

grep -rn "next/headers" --include="*.ts" --include="*.tsx" .

2Fix

Read the cookies on the server and send them in a Cookie header.

import { cookies } from "next/headers";

export default async function Page() {
  const cookieStore = await cookies();
  const res = await fetch("https://api.example.com/me", {
    headers: { Cookie: cookieStore.toString() },
  });
  const me = await res.json();
  return <p>{me.name}</p>;
}

3Verify

Load the page signed in and check that the API now returns the user.

npm run dev

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • Keep the API and the app on the same site so cookies stay first-party.
  • Never answer credentialed requests with Access-Control-Allow-Origin *.
  • Do auth-bound fetches on the server and pass cookies explicitly.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.