A NextAuth credentials login sends the browser back to /api/auth/signin?csrf=true
http://localhost:3000/api/auth/signin?csrf=trueShort answer
NextAuth 4 sends you to signin?csrf=true when the POST to /api/auth/callback/credentials arrives without a CSRF token that matches the CSRF cookie. The usual reasons are a hand-written form or fetch that sends no csrfToken, a NEXTAUTH_URL that says https while you browse over http, or a secret that changed.
Fix it fast
1Find out which cause you have
Send the credentials request by hand with no token and read the Location header. It shows the base address NextAuth builds its redirects from, and it proves the check is the CSRF one.
curl -si -X POST http://localhost:3000/api/auth/callback/credentials | grep -i '^location'2Match what you see, then jump to its fix
- 1
You wrote your own <form action="/api/auth/callback/credentials"> or a fetch call, and the Network tab shows a request body with no csrfToken fieldA custom form or fetch posts without the csrfTokenFix - 2
The Location header from the first step starts with https://localhost:3000, or the redirect target does not load at allNEXTAUTH_URL says https while the app is served over httpFix - 3
The first login attempt after a restart or deploy fails with the same redirect, a reload fixes it for a while, or it fails only on some requests behind a load balancerThe secret changed, or differs between server instancesFix
- 1
What does the error look like?
http://localhost:3000/api/auth/signin?csrf=truehttps://localhost:3000/api/auth/signin?csrf=true
Cause 1 of 3
A custom form or fetch posts without the csrfToken
1Check
For credentials providers, NextAuth refuses the POST unless the body carries a csrfToken that matches the one stored in the cookie. Only your own code can add it when you bypass signIn().
grep -rn "callback/credentials" app pages components src 2>/dev/null2Fix
Call signIn('credentials', ...) from next-auth/react, which fetches the token for you. If you must post a form yourself, read the token with getCsrfToken and put it in a hidden field named csrfToken.
// option 1: let the library do it
import { signIn } from 'next-auth/react'
await signIn('credentials', { redirect: false, username, password })
// option 2: a hand-written form (pages router)
import { getCsrfToken } from 'next-auth/react'
export default function SignIn({ csrfToken }) {
return (
<form method="post" action="/api/auth/callback/credentials">
<input name="csrfToken" type="hidden" defaultValue={csrfToken} />
<input name="username" type="text" />
<input name="password" type="password" />
<button type="submit">Sign in</button>
</form>
)
}
export async function getServerSideProps(context) {
return { props: { csrfToken: await getCsrfToken(context) } }
}3Verify
Submit the form with correct details; the browser no longer lands on signin?csrf=true. A wrong password now gives the normal credentials error instead.
curl -s http://localhost:3000/api/auth/csrfConfirmed by next-auth.js.org
Cause 2 of 3
NEXTAUTH_URL says https while the app is served over http
1Check
NextAuth builds its redirect addresses from the canonical site URL you set. An https address also switches on secure cookies, which are only accessible from https URLs, so the CSRF cookie never comes back on a plain http request.
grep -n "NEXTAUTH_URL" .env .env.local .env.development 2>/dev/null2Fix
Use the address you really open in the browser. For local development that is the http address; use https only where the site is served over https.
# .env.local
NEXTAUTH_URL=http://localhost:30003Verify
Restart the dev server and repeat the curl line of the first step: the Location header now starts with http://localhost:3000.
curl -si -X POST http://localhost:3000/api/auth/callback/credentials | grep -i '^location'Confirmed by next-auth.js.org
Cause 3 of 3
The secret changed, or differs between server instances
1Check
NextAuth stores the CSRF token in a cookie together with a hash made with the secret. If the stored hash does not match what the current secret produces, the cookie is discarded and a new token is issued, so a token that the browser still holds fails the check once.
grep -rn "NEXTAUTH_SECRET" .env .env.local .env.production 2>/dev/null2Fix
Set one NEXTAUTH_SECRET value and use the same value on every instance and in every deploy. Keep it stable; rotating it logs out users and invalidates open forms.
# generate once
openssl rand -base64 32
# .env.production, identical on every instance
NEXTAUTH_SECRET=<the generated value>3Verify
Restart the app, load the sign-in page and log in on the first attempt, then restart again and repeat.
curl -s http://localhost:3000/api/auth/csrfConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Prefer signIn() from next-auth/react over hand-written requests to the callback route.
- Keep NEXTAUTH_URL equal to the address users really open in each environment.
- Store NEXTAUTH_SECRET in the host's secret manager and never regenerate it on deploy.
- Test the login once after every change of environment variables.
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
Back end
Show a custom sign-in error in NextAuth without leaving the page
http://localhost:3000/api/auth/error?error=...
By default a failed NextAuth credentials sign-in navigates to the error page with the message in the URL.
3 causes, each with a fixWeb apps
NextAuth throws ERR_INVALID_URL only after deploy
Sign-in works with npm run dev but the same code fails after deploy
In production NextAuth.js needs to know the site's own address.
3 causes, each with a fixWeb apps
NextAuth logs NO_SECRET, or throws it in production
In production, we expect you to define a secret property in your configuration
NextAuth needs a secret to encrypt its JWT and hash tokens.
2 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 fix