Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Show a custom sign-in error in NextAuth without leaving the page

Errorhttp://localhost:3000/api/auth/error?error=...

Short answer

By default a failed NextAuth credentials sign-in navigates to the error page with the message in the URL. Call signIn with redirect: false, read the error field of the result, show it in your own form, and navigate yourself on success. This works for the credentials and email providers only.

  • Applies to 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

    Look at how your form calls signIn. Without redirect: false, the browser leaves the page whatever the result is.

    grep -rn "signIn(" app components pages 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1A wrong password lands on the NextAuth error page, or reloads the sign-in page with ?error= in the URLsignIn is called with the default redirect behaviourFix
    2. 2After adding redirect: false, a correct login seems to do nothingThe success path is no longer navigatedFix
    3. 3The option has no effect for Google, GitHub or other OAuth buttonsredirect: false is used with an OAuth providerFix

What does the error look like?

  • http://localhost:3000/api/auth/error?error=...

Cause 1 of 3

signIn is called with the default redirect behaviour

1Check

Throwing an Error from authorize sends the user to the error page with the message as a query parameter.

grep -rn "throw new Error" pages/api/auth app/api/auth 2>/dev/null

2Fix

Pass redirect: false. The promise then resolves with error, status, ok and url, so your form can display the message itself.

import { signIn } from 'next-auth/react'

const result = await signIn('credentials', {
  redirect: false,
  email,
  password,
})
if (result?.error) setMessage(result.error)

3Verify

Enter a wrong password; the page stays and your message appears.

# browser: the address bar does not change after the failed attempt

Confirmed by next-auth.js.org

Did this fix it?

Cause 2 of 3

The success path is no longer navigated

1Check

With redirect: false the library does not navigate at all. On success the result has ok set to true and url set to where the user would have gone; on failure url is null.

grep -rn "result.ok\|result?.ok" app components pages 2>/dev/null

2Fix

Check ok and move the user yourself.

if (result?.ok && result.url) {
  window.location.assign(result.url)
}

3Verify

A correct login now lands on the page you expect.

# browser: the dashboard page opens after a valid login

Confirmed by next-auth.js.org

Did this fix it?

Cause 3 of 3

redirect: false is used with an OAuth provider

1Check

The option is documented for the credentials and email providers only. OAuth sign-in always leaves your site for the provider.

grep -rn "signIn('" app components pages 2>/dev/null

2Fix

Use redirect: false only on the credentials or email form, and call OAuth providers with the plain form of signIn.

await signIn('credentials', { redirect: false, email, password })
await signIn('github')

3Verify

The credentials form stays on the page after a failure; the OAuth button still leaves for the provider.

# browser: compare the two buttons after a failed attempt

Confirmed by next-auth.js.org

Did this fix it?

How do I stop it coming back?

  • Throw an Error with a short, user-safe message from authorize, and never include secrets or database details.
  • Handle result.error, result.ok and result.url explicitly in the form.
  • Test one wrong password and one correct login after every auth change.

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.