Maaz Zindani

Loading the index…

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

NextAuth credentials login answers 401 Unauthorized with error=CredentialsSignin

Errorerror=CredentialsSignin

Short answer

In NextAuth 4 the callback route answers 401 when your authorize function returned null or threw. Log which fields reach it, then check three things: the user lookup or password check, the field names your form sends, and any exception inside authorize. A 403 is a different problem, in the signIn callback.

  • 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

    Print which fields arrive in authorize, names only and never the values. If a field you expect is missing from the list, the form is the problem; if all are there, the problem is inside authorize.

    async authorize(credentials) {
      console.log('authorize received', Object.keys(credentials ?? {}))
      // ...your lookup and check
    }
  2. 2Match what you see, then jump to its fix

    1. 1The server log shows no exception, the expected fields arrive, and the response says CredentialsSigninauthorize returns nullFix
    2. 2The first-step log lists keys such as email and pass, while authorize reads username and passwordThe form sends different field names than authorize readsFix
    3. 3The server log shows a stack trace from authorize, and the error in the response is that message rather than CredentialsSigninauthorize throws instead of returningFix

What does the error look like?

  • error=CredentialsSignin
  • { error: 'CredentialsSignin', status: 401, ok: false, url: null }

Cause 1 of 3

authorize returns null

1Check

NextAuth treats a null result as a failed login and answers with status 401 and the CredentialsSignin code. That covers an unknown user, a wrong password, and a comparison that never succeeds because of a bug in your check.

async authorize(credentials) {
  const user = await findUserByEmail(credentials?.email)
  console.log('user found:', Boolean(user))
  // log the boolean result of the password check the same way
}

2Fix

Make the lookup and the check return what you expect for a valid user, and return an object with at least an id. Return null only when the details really are wrong.

async authorize(credentials) {
  const user = await findUserByEmail(credentials?.email)
  if (!user) return null

  const valid = await verifyPassword(credentials.password, user.passwordHash)
  if (!valid) return null

  return { id: user.id, name: user.name, email: user.email }
}

3Verify

Log in with a user you know is valid: the signIn result has ok set to true and no error.

# in your form handler, the signIn result: ok is true and error is undefined

Confirmed by next-auth.js.org

Did this fix it?

Cause 2 of 3

The form sends different field names than authorize reads

1Check

The keys of the credentials object are the fields that the provider accepts, and authorize receives the submitted values under those names. A value sent under another name arrives as undefined, so the check fails and the login returns 401. A form library that keeps its values under its own names makes this easy to miss.

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

2Fix

Send the values under the names that the provider declares. With a form library, pass its values object and add redirect: false.

// provider
credentials: {
  email: { label: 'Email', type: 'text' },
  password: { label: 'Password', type: 'password' },
},

// submit handler
await signIn('credentials', { ...values, redirect: false })

3Verify

The first-step log now lists email and password, and a valid login goes through.

# server log: authorize received [ 'email', 'password', ... ]

Confirmed by next-auth.js.org

Did this fix it?

Cause 3 of 3

authorize throws instead of returning

1Check

When authorize throws, NextAuth also answers with status 401 and passes the error message on. A database that is not reachable, a JSON parse problem or an unhandled rejection all end this way.

grep -n "throw " app/api/auth/\[...nextauth\]/route.ts pages/api/auth/\[...nextauth\].ts 2>/dev/null

2Fix

Wrap the work in try and catch. Return null for a normal failed login, throw only for a message you want users to see, and log anything unexpected on the server.

async authorize(credentials) {
  try {
    // lookup and password check
  } catch (err) {
    console.error('authorize failed', err)
    return null
  }
}

3Verify

Stop the database and try again: the log shows the real error once, and the sign-in form shows the normal failure message.

# server log: authorize failed ...

Confirmed by next-auth.js.org

Did this fix it?

How do I stop it coming back?

  • Keep field names in the provider's credentials object, the form and the signIn call identical, and name them once in a shared constant.
  • Decide on purpose which failures return null and which throw, and document it next to authorize.
  • Never log passwords while debugging; log keys and yes/no results.
  • Remember the codes: 401 with CredentialsSignin comes from authorize, 403 with AccessDenied comes from the signIn 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

Next step

Want this handled for good?

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