Show a custom sign-in error in NextAuth without leaving the page
http://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.
Fix it fast
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/null2Match what you see, then jump to its fix
- 1
A 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
After adding redirect: false, a correct login seems to do nothingThe success path is no longer navigatedFix - 3
The option has no effect for Google, GitHub or other OAuth buttonsredirect: false is used with an OAuth providerFix
- 1
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/null2Fix
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 attemptConfirmed by next-auth.js.org
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/null2Fix
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 loginConfirmed by next-auth.js.org
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/null2Fix
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 attemptConfirmed by next-auth.js.org
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
Web apps
NextAuth CLIENT_FETCH_ERROR: the session call returned HTML
Unexpected token '<', "<!DOCTYPE "... is not valid JSON
The NextAuth client asks /api/auth/session for JSON and received an HTML page instead, usually a 404 or an error page.
2 causes, each with a fixBack end
create-next-app keeps creating a TypeScript project
Would you like to use TypeScript? No / Yes
create-next-app now makes a TypeScript project by default.
2 causes, each with a fixBack end
Request body is rejected as too large in Next.js (1 MB default)
413 Payload Too Large
Next.js limits request bodies to 1 MB by default, in both Pages Router API routes and Server Actions.
3 causes, each with a fixBack end
Next.js server code cannot connect to its own API on 127.0.0.1
Error: connect ECONNREFUSED 127.0.0.1:3003
ECONNREFUSED means nothing was listening at that address and port when the server-side fetch ran.
2 causes, each with a fixBack end
next build stops with: Failed to collect page data for a route
Error: Failed to collect page data for /dashboard
Failed to collect page data for a route is the last line of a failed build, not the cause.
3 causes, each with a fixBack end
TypeError: Failed to parse URL when a Next.js server fetches its own API
TypeError: Failed to parse URL from undefined/api/test
Server code has no page address, so fetch('/api/test') cannot be turned into a full URL and throws Failed to parse URL.
3 causes, each with a fix