Maaz Zindani

Loading the index…

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

NextAuth sends the user to ?error=AccessDenied after Google sign in

ErrorAccessDenied

Short 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.

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

Fix it fast

  1. 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
      },
    }
  2. 2Match what you see, then jump to its fix

    1. 1'signIn result false' in the log, or no signIn callback logic reaches return trueThe signIn callback returns falseFix
    2. 2'signIn start' is printed but 'signIn result' never is, and an exception shows in the server logThe callback throws an error while it runsFix

What does the error look like?

  • AccessDenied
  • error=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/null

2Fix

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 in

Confirmed by authjs.dev

Did this fix it?

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 in

Confirmed by authjs.dev

Did this fix it?

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.