Maaz Zindani

Loading the index…

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

A NextAuth credentials login sends the browser back to /api/auth/signin?csrf=true

Errorhttp://localhost:3000/api/auth/signin?csrf=true

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

  • 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

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

    1. 1You 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. 2The 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. 3The 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

What does the error look like?

  • http://localhost:3000/api/auth/signin?csrf=true
  • https://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/null

2Fix

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/csrf

Confirmed by next-auth.js.org

Did this fix it?

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/null

2Fix

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:3000

3Verify

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

Did this fix it?

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/null

2Fix

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/csrf

Confirmed by raw.githubusercontent.com

Did this fix it?

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

Next step

Want this handled for good?

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