# NextJS - CORS No 'Access-Control-Allow-Origin' header is present on the requested resource

**URL:** <https://community.auth0.com/t/nextjs-cors-no-access-control-allow-origin-header-is-present-on-the-requested-resource/125578>\
**Category:** Get Help\
**Tags:** configuration, application\
**Created:** [January 24, 2024, 1:10pm UTC](https://community.auth0.com/t/nextjs-cors-no-access-control-allow-origin-header-is-present-on-the-requested-resource/125578 "2024-01-24T13:10:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![noelt.dolan](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/noelt.dolan/32/60288_2.png) [@noelt.dolan](https://community.auth0.com/u/noelt.dolan)\
**Post date:** [January 24, 2024, 1:10pm UTC](https://community.auth0.com/t/nextjs-cors-no-access-control-allow-origin-header-is-present-on-the-requested-resource/125578/1 "2024-01-24T13:10:12Z")

</div>

Hi,

I’m having a problem with auth0 CORS issues both on local and deployed development branch of NextJS 14 app.

In my auth0 applicaton account I’ve added both my local and deployed development urls under the following sections:

Allowed Callback URLs,  
Allowed Logout URLs,  
Allowed Web Origins  
Allow Cross-Origin Authentication  
Allowed Origins (CORS)

But I’m still getting the following just for home, this one is for localhost:

_"Access to fetch at ‘[https://dev-my-instanceId.us.auth0.com/authorize?](https://dev-my-instanceId.us.auth0.com/authorize?)…’ (redirected from ‘[http://localhost:3000/dashboard?\_rsc=acgkz](http://localhost:3000/dashboard?_rsc=acgkz)’) from origin ‘[http://localhost:3000](http://localhost:3000)’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource._ "

```auto
Access to fetch at 'https://dev-<instanceId>.us.auth0.com/authorize?client_id=<clientId>>&scope=openid%20profile%20email&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fapi%2Fauth%2Fcallback&nonce=oIdCmaHiR6DVq96O8sNnF5KfgBrjXaE4gNevlbhPujg&state=eyJyZXR1cm5UbyI6Imh0dHA6Ly9sb2NhbGhvc3Q6MzAwMC9kYXNoYm9hcmQifQ&code_challenge_method=S256&code_challenge=192mQQ_qp_u0IyOnjDBiH6ptWQhOWeQqeCZ5ZDQ49E4' (redirected from 'http://localhost:3000/dashboard?_rsc=acgkz') from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

```

For reference, this is what my home/landing page looks like.

```auto
'use client';
import { useUser } from '@auth0/nextjs-auth0/client';
import Link from 'next/link';

export default function Home() {
  const { user, isLoading, error } = useUser();

  if (isLoading) {
    return (
      <div>
        <p className="text-warn">loading...!:p</p>
      </div>
    );
  }

  if (error) {
    return (
      <div>
        <p className="text-warn">
          Something gone very wrong! error:{JSON.stringify(error, null, 4)}
        </p>
      </div>
    );
  }

  return (
    <>
      <h1 className="text-3xl font-bold underline">Home page!</h1>
      {!user ? (
        <p>
          Looks like you need to <Link href="/api/auth/login">login</Link>{' '}
          first!
        </p>
      ) : (
        <p>hello {user.name}!</p>
      )}
    </>
  );
}

```

Anyone else having this problem? Am I missing something obvious? (can’t believe I’d be the only one)

---

<div class="post-metadata">

**Author:** ![noelt.dolan](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/noelt.dolan/32/60288_2.png) [@noelt.dolan](https://community.auth0.com/u/noelt.dolan)\
**Post date:** [January 24, 2024, 1:13pm UTC](https://community.auth0.com/t/nextjs-cors-no-access-control-allow-origin-header-is-present-on-the-requested-resource/125578/2 "2024-01-24T13:13:05Z")

</div>

Also, for further reference, here is the basic /api/auth/[auth0]/route.ts setup.

```auto
import { handleAuth, handleLogin, handleLogout } from '@auth0/nextjs-auth0';

const logoutUrl = [
  `${process.env.AUTH0_ISSUER_BASE_URL}/v2/logout?`,
  `client_id=${process.env.AUTH0_CLIENT_ID}`,
  `&returnTo=${process.env.AUTH0_BASE_URL}`,
];

export const GET = handleAuth({
  login: handleLogin({
    returnTo: '/',
  }),
  logout: handleLogout({
    returnTo: logoutUrl.join(''),
  }),
});

```

---

<div class="post-metadata">

**Author:** ![noelt.dolan](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/noelt.dolan/32/60288_2.png) [@noelt.dolan](https://community.auth0.com/u/noelt.dolan)\
**Post date:** [January 26, 2024, 9:26am UTC](https://community.auth0.com/t/nextjs-cors-no-access-control-allow-origin-header-is-present-on-the-requested-resource/125578/3 "2024-01-26T09:26:06Z")

</div>

Hmmm… looks like this is a long standing issue!

> <https://github.com/auth0/nextjs-auth0/issues/757>
>
> \<!--
> \*\*Please do not report security vulnerabilities here\*\*. The Responsible Di…sclosure Program (https://auth0.com/whitehat) details the procedure for disclosing security issues.
> 
> Thank you in advance for helping us to improve this library! Please read through the template below and answer all relevant questions. Your additional work here is greatly appreciated and will help us respond as quickly as possible. For general support or usage questions, use the Auth0 Community (https://community.auth0.com/) or Auth0 Support (https://support.auth0.com/). Finally, to avoid duplicates, please search existing Issues before submitting one here.
> 
> By submitting an Issue to this repository, you agree to the terms within the Auth0 Code of Conduct (https://github.com/auth0/open-source-template/blob/main/CODE-OF-CONDUCT.md).
> \--\>
> 
> \### Describe the problem
> When logging out a user, CORS errors prevent redirect.
> 
> Login redirection works as intended...
> 
> Errors in console:
> \`\`\`
> Application error: a client-side exception has occurred (see the browser console for more information).
> Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://auth.dev.mydomain.com/v2/logout?returnTo=https%3A%2F%2Ftest-app.mydomain.com&client\_id=XXXXXX. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 404.
> 
> 22:24:10.987 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://auth.dev.mydomain.com/v2/logout?returnTo=https%3A%2F%2Ftest-app.mydomain.com&client\_id=XXXXXX. (Reason: CORS request did not succeed). Status code: (null).
> \`\`\`
> 
> I have double checked all applicable settings for my tenant:
> \- \[x\] Allowed Logout URLs contains https://test-app.mydomain.com
> \- \[x\] Allowed Web Origins contains https://test-app.mydomain.com
> \- \[x\] Allowed Callback URLs containts https://test-app.mydomain.com/api/auth/callback
> \<!--
> \> Provide a clear and concise description of the issue
> \--\>
> 
> \### What was the expected behavior?
> App should redirect to https://test-app.mydomain.com
> \<!--
> \> Tell us about the behavior you expected to see
> \--\>
> 
> \### Reproduction
> 
> \<!--
> \> Detail the steps taken to reproduce this error, and whether this issue can be reproduced consistently or if it is intermittent.
> \> \*\*Note\*\*: If clear, reproducable steps or the smallest sample app demonstrating misbehavior cannot be provided, we may not be able to follow up on this bug report.
> 
> \> Where possible, please include:
> \>
> \> - The smallest possible sample app that reproduces the undesirable behavior
> \> - Log files (redact/remove sensitive information)
> \> - Application settings (redact/remove sensitive information)
> \> - Screenshots
> \--\>
> 
> \### Environment
> 
> \<!--
> \> Please provide the following:
> \--\>
> 
> \- \*\*Version of this library used:\*\*
> - Auth0 NextJS version: ^1.9.1
> \- \*\*Which framework are you using, if applicable:\*\*
> - Next.JS version: ^12.2.0
> \- \*\*Any other relevant information you think would be useful:\*\*
> - Component:
> \`\`\`jsx
> \<Link href="/api/auth/logout" passHref\>
> \<a\>Logout\</a\>
> \</Link\>
> \`\`\`
> - I have grafana setup via the same tenant and application where login/logout works as intended.

It’s not really impressive that something like this is left unfixed.

---

<div class="post-metadata">

**Author:** ![samuel5](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@samuel5](https://community.auth0.com/u/samuel5)\
**Post date:** [April 24, 2024, 6:49pm UTC](https://community.auth0.com/t/nextjs-cors-no-access-control-allow-origin-header-is-present-on-the-requested-resource/125578/4 "2024-04-24T18:49:06Z")

</div>

Came here battling the same issue, always Cors errors on logout (and always in my production environment, never with local developement 🤷‍♂️ ).

The only fix I have found is to use a _clean_ a tag:

`<a href="/api/auth/logout/">Logout</a>`

Link component or using router.push(“api/auth/logout”) doesnt work due to the _prefetch_ that the browser will do (I was able to capture this in the network tab inside dev tools).

The readme for auth0/nextjs also uses a href tag:  
[https://github.com/auth0/nextjs-auth0#consume-authentication](https://github.com/auth0/nextjs-auth0#consume-authentication)

---

<div class="post-metadata">

**Author:** ![turbowars](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/turbowars/32/60141_2.png) [@turbowars](https://community.auth0.com/u/turbowars)\
**Post date:** [September 8, 2024, 9:56am UTC](https://community.auth0.com/t/nextjs-cors-no-access-control-allow-origin-header-is-present-on-the-requested-resource/125578/5 "2024-09-08T09:56:38Z")

</div>

Stumbled on the same issue Gentleman. I am assuming you guys are also using the free tier and are redirecting it to auth0 servers to authorize. Do you think it will be solved when hosted on our domain?

---

<div class="post-metadata">

**Author:** ![ale.vesga1](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ale.vesga1/32/60921_2.png) [@ale.vesga1](https://community.auth0.com/u/ale.vesga1)\
**Post date:** [November 19, 2024, 6:18pm UTC](https://community.auth0.com/t/nextjs-cors-no-access-control-allow-origin-header-is-present-on-the-requested-resource/125578/6 "2024-11-19T18:18:08Z")

</div>

Hi @noelt.dolan,

Your application is making a CORS request to the /authorize endpoint instead of redirecting the user’s browser, which is leading to a CORS error as the /authorize endpoint does not support CORS.

Exactly as @samuel5 found, the recommended fix is to use anchor (`<a>`) tags instead of Link tags when authenticating users. Using Link tags causes Javascript to call /authorize instead of redirecting the user’s view.

See these resources:

- [Consume Authentication](https://github.com/auth0/nextjs-auth0?tab=readme-ov-file#consume-authentication)
- [CORS error when initiating silent auth requests](https://community.auth0.com/t/cors-error-when-initiating-silent-auth-requests/103208)
- [Sync session with Auth0 when using Next.js](https://community.auth0.com/t/sync-session-with-auth0-when-using-next-js/114256)
- [Cross-Origin Authentication](https://auth0.com/docs/authenticate/login/cross-origin-authentication)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [December 3, 2024, 6:19pm UTC](https://community.auth0.com/t/nextjs-cors-no-access-control-allow-origin-header-is-present-on-the-requested-resource/125578/7 "2024-12-03T18:19:03Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
