The CORS error with credentials almost always comes from one conflict: your frontend sends credentials: 'include' (or withCredentials: true), but your server answers with Access-Control-Allow-Origin: *. Browsers refuse to pair a wildcard origin with credentials, full stop. The fix is to echo the exact requesting origin and send Access-Control-Allow-Credentials: true instead.
I run into this constantly in client projects: a single-page app on one subdomain calls an API on another, someone adds credentials: 'include' to get cookies or an Authorization header through, and the console lights up with "No 'Access-Control-Allow-Origin' header" or "expected 'true' in CORS header 'Access-Control-Allow-Credentials'". The request often already succeeded on the server. The browser is just refusing to hand the response to your JavaScript.
Why the CORS error with credentials happens
CORS is enforced entirely by the browser, not your server. The Fetch Standard's CORS protocol lets a server opt specific origins into reading its responses, and the browser checks the response headers after the request already ran. That's why your backend logs show a clean 200 while the browser still throws a CORS error — the server did its job, the browser just withheld the body.
Credentialed requests add a stricter rule. Per MDN's documentation on the missing-credentials error, when a request is sent with credentials, the server must set Access-Control-Allow-Credentials to the literal string true — not "True", not omitted — or the browser discards the response even if every other header looks right. And a credentialed request can never be answered with a wildcard origin: the server must name the exact origin that's allowed to read the response.
Five response headers control the whole exchange. Get these wrong and nothing else matters.
| Header | Required value with credentials | Common mistake |
|---|
Access-Control-Allow-Origin | The exact requesting origin, never * | Leaving it as a wildcard |
Access-Control-Allow-Credentials | true | Omitting it, or setting false |
Access-Control-Allow-Methods | Every non-simple method you use (PUT, PATCH, DELETE) | Forgetting methods beyond GET/POST |
Access-Control-Allow-Headers | Every custom header, including Authorization | Assuming * covers Authorization (it never does) |
Vary | Origin | Skipping it when origin is reflected dynamically |
How to fix it: step by step
- Confirm the request is actually credentialed. Check the frontend code for
credentials: 'include' (fetch) or xhr.withCredentials = true. If you don't need cookies or session auth, the simplest fix is to drop credentials entirely and use a bearer token in a header instead — no credentials, no wildcard restriction.
- Build an explicit origin allowlist on the server. Never hardcode
* once credentials are involved. Read the incoming Origin header, compare it against your allowlist, and echo back only the matching value.
- Set
Access-Control-Allow-Credentials: true on both the preflight OPTIONS response and the real response. One without the other still fails.
- Add
Vary: Origin so a CDN or shared cache doesn't serve one origin's CORS headers to a different origin — a bug that looks like CORS failing intermittently for no reason, as the Phenomic engineering blog describes when origin-reflecting servers skip this header.
- List every non-simple header and method explicitly. If your frontend sends
Authorization, Content-Type: application/json, or a custom X-Request-Id, each one must appear in Access-Control-Allow-Headers.
- Re-test with the browser console open, not just curl or Postman — those tools never enforce CORS and will tell you nothing about whether the browser will accept the response.
A minimal, correct Express example:
const allowedOrigins = ["https://app.example.com", "http://localhost:5173"];
app.use((req, res, next) => {
const origin = req.headers.origin;
if (allowedOrigins.includes(origin)) {
res.setHeader("Access-Control-Allow-Origin", origin);
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Vary", "Origin");
}
res.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,PATCH,DELETE");
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
if (req.method === "OPTIONS") return res.sendStatus(204);
next();
});
Why does my preflight pass but the real request still fail?
This is one of the most common follow-up bugs. Teams configure CORS headers only inside their OPTIONS handler and forget to add them to the actual GET or POST handler that runs afterward. The browser checks CORS headers twice — once on the preflight, once on the real response — and credentials make both checks strict. If your framework handles preflight and the real request in separate code paths, both paths need the full header set, not just the preflight.
Why does curl or Postman work but the browser rejects the same request?
Because curl and Postman never enforce the same-origin policy; they send the request and read whatever comes back. Only browsers run the CORS check. A request that returns 200 with a full body in Postman can still fail in the browser if the response is missing Access-Control-Allow-Origin or sends it as * alongside credentials. Treat a successful Postman test as proof the endpoint works — never as proof your CORS headers are correct.
Does this stop CSRF attacks too?
No, and this is worth understanding if you're relying on CORS for security. A simple cross-origin POST with a form-compatible content type is sent and executed by your server before the browser ever evaluates the CORS headers — CORS only decides whether the calling page's JavaScript gets to read the response, not whether the request runs. Use CSRF tokens or SameSite cookies for that problem; don't ask CORS to do a job it was never designed for.
Cross-site cookies and Safari
If your frontend and API live on genuinely different sites rather than subdomains of the same domain, cookies in a credentialed CORS request also depend on your cookie attributes, not just your CORS headers. A cookie sent cross-site needs SameSite=None and Secure, and even then, browsers with strict third-party cookie policies may still block it regardless of correct CORS configuration. When that happens, switching the API to a same-site subdomain, or moving to an Authorization header token instead of a cookie, removes the whole category of failure.
This is the kind of cross-cutting infrastructure decision I handle for clients doing web app development — getting auth, cookies, and cross-origin API calls right from day one avoids weeks of production debugging later. It overlaps with webhook raw-body handling too: I covered a similar "the middleware already changed the data before verification ran" failure mode in Stripe webhook signature verification failed, and the fix pattern — keep the data in its original form until the check that needs it runs — is the same lesson in both cases.
A quick debugging checklist
- Open the browser console first; it names the exact failing rule (origin, credentials, headers, or method), unlike a generic
fetch rejection.
- Open the Network tab and check for a separate
OPTIONS request. If present, verify headers on both the preflight and the real response.
- Confirm
Access-Control-Allow-Origin is the exact origin, never *, whenever Access-Control-Allow-Credentials: true is also present.
- Check for 4xx/5xx responses on the preflight — an error response without CORS headers produces the same browser error as a missing-header misconfiguration.
- Rule out mixed content: fetching
http:// from an https:// page fails regardless of CORS headers.
- Test in a clean browser profile to rule out an extension silently cancelling the request before CORS is even evaluated.
This same "check both server state and browser state separately" discipline is how I approach most production authentication bugs, including the token-scope confusion covered in the OAuth audience injection fix and the middleware-ordering bugs behind the Next.js proxy.ts migration — in both cases, the error message points at the symptom, not the actual misconfigured layer.
Multiple frontend origins: do it safely
If you support several frontend origins (a marketing site, an app, a staging environment), resist the temptation to list them all in one Access-Control-Allow-Origin header — the header accepts exactly one value or *, never a comma-separated list. Instead, keep the allowlist in server-side config, compare the incoming Origin against it on every request, and echo back only the match. Always pair this with Vary: Origin so caches don't leak one origin's allowed response to another.
Frequently asked questions
Can I use Access-Control-Allow-Origin: * with cookies?
No. The browser rejects a wildcard origin on any credentialed request, and it also treats * as a literal (non-matching) value in Access-Control-Allow-Headers, Access-Control-Allow-Methods, and Access-Control-Expose-Headers. Echo the specific requesting origin instead.
Why does my fetch fail with "Failed to fetch" and no other detail?
The fetch API rejects with a generic TypeError on any CORS failure; the specific reason (missing header, wrong credentials flag, disallowed method) only shows up in the browser's console, not in your JavaScript's catch block. Always check DevTools, not just your error logging.
Does setting credentials to 'same-origin' avoid this entirely?
Yes, if your frontend and API truly share the same origin (identical scheme, host, and port). If they're on different subdomains or ports, the request is still cross-origin and needs full CORS headers regardless of the credentials setting.
No. CORS headers are a server responsibility. Frontend code can only opt into sending credentials or custom headers; it cannot grant itself permission to read a cross-origin response. Fixing a CORS error always means changing server configuration, not frontend fetch options (aside from removing unnecessary credentials).
Why did this start failing only in production, not locally?
Local development often runs the frontend and API on the same machine with permissive defaults, or developers use a dev-server proxy that hides CORS entirely. Production typically puts the frontend behind a CDN and the API behind a different domain, which exposes a cross-origin configuration that was never actually tested until deploy day.
Key takeaways
- A CORS error with credentials means a wildcard
Access-Control-Allow-Origin is colliding with credentials: 'include' or withCredentials: true — browsers never allow that pairing.
- Fix it by echoing the exact requesting origin, setting
Access-Control-Allow-Credentials: true, and adding Vary: Origin on every response, including the preflight.
- CORS is enforced by the browser only; curl, Postman, and your server logs will not reveal a CORS misconfiguration.
- CORS does not protect against CSRF — a simple cross-origin POST still reaches and executes on your server before any CORS header is checked.
- In 2026, with more apps split across subdomains and CDNs, testing cross-origin credentialed requests in a real browser before shipping is cheaper than debugging it in production.