next-auth asks you to sign in with a different account
/api/auth/signin?error=OAuthAccountNotLinkedShort 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.
Fix it fast
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=OAuthAccountNotLinked2Match what you see, then jump to its fix
- 1
error=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
error=AccessDeniedThe signIn callback rejects the userFix - 3
error=OAuthCallbackError, working on localhost only.The provider rejects the callback, usually the production callback URLFix - 4
Sign-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
- 1
What does the error look like?
/api/auth/signin?error=OAuthAccountNotLinkederror=AccessDeniederror=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 provider2Fix
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 barConfirmed by authjs.dev
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/null2Fix
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 barConfirmed by authjs.dev
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/google2Fix
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/google3Verify
Sign in on the deployed site.
# repeat the sign-in and read the address barConfirmed by authjs.dev
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=true3Verify
Restart the server and sign in again.
# repeat the sign-in and read the address barConfirmed by authjs.dev
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
Web apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fixWeb apps
Firebase Auth says the API key is not valid
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
The apiKey your app sends is either empty, wrong, or blocked.
4 causes, each with a fixWeb apps
Jest fails with SyntaxError: Unexpected token when testing a Next.js component
Jest failed to parse a file.
Jest only understands plain JavaScript until a transform and a few file mappings tell it more.
3 causes, each with a fix