NextAuth credentials login answers 401 Unauthorized with error=CredentialsSignin
error=CredentialsSigninShort 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.
Fix it fast
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 }2Match what you see, then jump to its fix
- 1
The server log shows no exception, the expected fields arrive, and the response says CredentialsSigninauthorize returns nullFix - 2
The 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
The server log shows a stack trace from authorize, and the error in the response is that message rather than CredentialsSigninauthorize throws instead of returningFix
- 1
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 undefinedConfirmed by next-auth.js.org
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/null2Fix
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
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/null2Fix
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
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
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 sends the user to ?error=AccessDenied after Google sign in
error=AccessDenied
AccessDenied is not a Google or MongoDB error.
2 causes, each with a fixWeb apps
TypeScript rejects the authorize function of the Credentials provider
A custom field you return from authorize is missing on the User or Session type
The authorize callback must return either a user object or null.
3 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