Maaz Zindani

Loading the index…

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

next-auth asks you to sign in with a different account

Error/api/auth/signin?error=OAuthAccountNotLinked

Short answer

The error=OAuthAccountNotLinked code means the email already belongs to a user created with another sign-in method, and Auth.js will not link it automatically while the visitor is signed out. Sign in the original way first, or enable email-based linking only for a provider that verifies emails.

  • Applies to Auth.js 5, next-auth 4, Next.js 15, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the error query parameter in the browser address bar. Each code below has its own cause, and the one in the URL tells you which section to use.

    # example of what the address bar shows after a failed sign-in
    /api/auth/signin?error=OAuthAccountNotLinked
  2. 2Match what you see, then jump to its fix

    1. 1error=OAuthAccountNotLinked. It appears when the production user table already holds that email from an earlier sign-in method.The email is already linked to a different providerFix
    2. 2error=AccessDeniedThe signIn callback rejects the userFix
    3. 3error=OAuthCallbackError, working on localhost only.The provider rejects the callback, usually the production callback URLFix
    4. 4Sign-in works locally and fails only when the app runs behind a reverse proxy.The app sits behind a proxy and does not trust the forwarded hostFix

What does the error look like?

  • /api/auth/signin?error=OAuthAccountNotLinked
  • error=AccessDenied
  • error=OAuthCallbackError

Cause 1 of 4

The email is already linked to a different provider

1Check

Auth.js does not link an OAuth account to an existing user with the same email when the visitor is not signed in. Look in your user store for that email and see which provider created the account.

# in your own user store: find the account whose email matches, and note its provider

2Fix

Have the user sign in with the original provider, then link the second one while signed in. Only if you trust the provider to verify email addresses, turn on automatic linking in its config.

import Google from "next-auth/providers/google";

Google({ allowDangerousEmailAccountLinking: true })

3Verify

Sign in with the second provider using the same email. The redirect goes to your app, not back to the sign-in page.

# repeat the sign-in in a private window and read the address bar

Confirmed by authjs.dev

Did this fix it?

Cause 2 of 4

The signIn callback rejects the user

1Check

Auth.js throws this when your signIn callback returns false or throws.

grep -rn "signIn" auth.ts auth.config.ts 2>/dev/null

2Fix

Make the callback return true for users who should get in, and handle its own errors.

callbacks: {
  async signIn({ user }) {
    return Boolean(user.email);
  },
}

3Verify

Sign in again with the account that was denied.

# repeat the sign-in and read the address bar

Confirmed by authjs.dev

Did this fix it?

Cause 3 of 4

The provider rejects the callback, usually the production callback URL

1Check

Each OAuth provider needs the production callback URL registered. With Next.js the default path is /api/auth/callback/[provider].

# provider console: authorised redirect URIs must contain
https://example.com/api/auth/callback/google

2Fix

Register the exact production callback URL with the provider. If it allows only one URL per app, create a separate OAuth app for each environment.

https://example.com/api/auth/callback/google

3Verify

Sign in on the deployed site.

# repeat the sign-in and read the address bar

Confirmed by authjs.dev

Did this fix it?

Cause 4 of 4

The app sits behind a proxy and does not trust the forwarded host

1Check

Behind a reverse proxy, Auth.js has to trust the X-Forwarded-Host header. It infers this on Vercel and Cloudflare Pages, but not elsewhere.

printenv | grep AUTH_

2Fix

Set AUTH_TRUST_HOST to true, and make sure AUTH_SECRET is set in production.

AUTH_SECRET=<output of: openssl rand -base64 33>
AUTH_TRUST_HOST=true

3Verify

Restart the server and sign in again.

# repeat the sign-in and read the address bar

Confirmed by authjs.dev

Did this fix it?

How do I stop it coming back?

  • Decide early whether one email can use several providers, and set linking on purpose.
  • Register localhost and production callback URLs with each provider before the first deploy.
  • Copy the production env variables from a checklist; AUTH_SECRET is the only strictly required one.

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.