NextAuth sends the user to ?error=AccessDenied after Google sign in
AccessDeniedShort answer
AccessDenied is not a Google or MongoDB error. Auth.js raises it when your signIn callback returns false or throws while it runs. Log inside the callback to see which path runs, return true for users you allow, and wrap database lookups so a failure does not escape.
Fix it fast
1Find out which cause you have
Add a log at the top and the end of your signIn callback. If the second line never prints, the callback threw; if it prints false, your own condition rejected the user.
callbacks: { async signIn({ user, account, profile }) { console.log('signIn start', user?.email, account?.provider) // ...your checks const ok = true console.log('signIn result', ok) return ok }, }2Match what you see, then jump to its fix
What does the error look like?
AccessDeniederror=AccessDenied
Cause 1 of 2
The signIn callback returns false
1Check
Read every return in the callback. An allowlist that compares the wrong field (email versus id), a missing return, or a condition that is false for new users all end as false. Returning false shows the error page.
grep -rn -A12 "signIn" app/api/auth pages/api/auth auth.ts auth.config.ts 2>/dev/null2Fix
Return true for the users who should be allowed. Return false only for the ones you want to block.
async signIn({ user }) {
const allowed = ['admin@example.com']
return allowed.includes(user.email ?? '')
}3Verify
Sign in with an allowed account; you should land on the callback URL instead of the error page.
# open /api/auth/signin in a private window and sign inConfirmed by authjs.dev
Cause 2 of 2
The callback throws an error while it runs
1Check
A database call inside the callback (a user lookup, a role query) rejects when the connection string is wrong or the database is unreachable. Auth.js reports an error thrown in this callback as AccessDenied.
# run the same query outside the callback
node -e "console.log(process.env.MONGODB_URI ? 'set' : 'missing')"2Fix
Fix the underlying error, then catch it in the callback so the decision stays explicit.
async signIn({ user }) {
try {
const dbUser = await findUserByEmail(user.email)
return Boolean(dbUser)
} catch (err) {
console.error('signIn lookup failed', err)
return false
}
}3Verify
The server log should show either a clean result or your own error line, not an unhandled exception.
# watch the terminal running next dev while you sign inConfirmed by authjs.dev
How do I stop it coming back?
- Keep the signIn callback short; do the allow or block decision in one place.
- Log the callback's decision so a rejected sign in is visible in the server log.
- Test both an allowed and a blocked account after every change to the callback.
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
next-auth asks you to sign in with a different account
error=AccessDenied
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.
4 causes, each with a fixWeb 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 fix