feat(auth): structured forensics on credentials sign-in failures

debug_logs forensics on auth was useless: six "Read more at
https://errors.authjs.dev#credentialssignin" entries clustered in a
2-minute window on 2026-05-20 with no way to tell brute-force from
forgotten-password from missing-account. Auth.js wraps every
authorize()→null in a generic CredentialsSignin error whose .message
is just the help URL — and the meta only carries the minified stack
(name:"w", type:"CredentialsSignin"), nothing about which provider
was tried or why it rejected.

Three Credentials providers in src/auth.ts (local, email-password,
user-password), each with 4 null-return paths. Replaced all 12
`return null` reject sites with `return logAuthReject(provider,
reason, identifier)` — a small helper that fire-and-forgets a warn-
level debug_logs row capturing:
  • provider:    "local" | "email-password" | "user-password"
  • reason:      "missing-input" | "user-not-found"
              | "no-password-hash" | "wrong-password"
  • identifier:  email or userId, NEVER the password

This makes "why can't I sign in" a one-query answer:

  SELECT meta FROM debug_logs
   WHERE source='auth.authorize' AND created_at > NOW() - INTERVAL '1h'

Operator can now distinguish:
  - 6 wrong-password for one email → forgotten password
  - 6 user-not-found for varying emails → enumeration/brute-force
  - 6 no-password-hash for one email → OAuth-only account hitting
    the password form (a real UX bug worth chasing)

Did NOT modify the catch-all logger at src/auth.ts:67 — it still
captures system errors (DB unreachable, etc.) where the generic
message is appropriate. This change targets the reject path only.

tsc + eslint + build clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
g-butandClaude Opus 4.7 committed 2026-05-21 05:59:11 +02:00
1 parent 1762d86ff2
commit 91a5989182
1 file changed
+34 -9
+34 -9
View File
@@ -12,6 +12,29 @@ import { users, accounts, sessions, verificationTokens } from "@/db/schema";
import { verifyPassword } from "@/lib/password";
import { getDefaultUser, getUserById, getUserByEmail, updateUser } from "@/db/queries/users";
import { getOrgMembershipCount, createPersonalOrg } from "@/db/queries/orgs";
import { logDebug } from "@/db/queries/debug-logs";
/**
* Auth.js wraps every authorize()→null return in a generic CredentialsSignin
* error whose message is just "Read more at https://errors.authjs.dev/...",
* which makes debug_logs forensics worthless when a user can't sign in.
* This helper records the actual failure reason (provider + reason + identifier
* minus password) BEFORE returning null, so the operator can tell apart
* brute-force vs forgotten-password vs missing-account at a glance.
*/
function logAuthReject(
provider: "local" | "email-password" | "user-password",
reason: "missing-input" | "user-not-found" | "no-password-hash" | "wrong-password",
identifier?: string | null,
): null {
logDebug({
source: "auth.authorize",
level: "warn",
message: `credentials rejected: ${provider} / ${reason}`,
meta: { provider, reason, identifier: identifier ?? null },
});
return null;
}
declare module "next-auth" {
interface Session {
@@ -127,10 +150,10 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
},
async authorize(credentials) {
const supplied = credentials.password as string | undefined;
if (!supplied) return null;
if (!supplied) return logAuthReject("local", "missing-input");
const user = await getDefaultUser();
if (!user) return null;
if (!user) return logAuthReject("local", "user-not-found");
// Env var takes priority (quick local dev). Falls back to DB hash (packaged installs).
const envPassword = process.env.LOCAL_AUTH_PASSWORD;
@@ -140,7 +163,7 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
? await verifyPassword(supplied, user.passwordHash)
: false;
if (!ok) return null;
if (!ok) return logAuthReject("local", envPassword || user.passwordHash ? "wrong-password" : "no-password-hash", user.email);
return { id: user.id, email: user.email ?? "", name: user.name ?? "Local user" };
},
}),
@@ -155,13 +178,14 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
async authorize(credentials) {
const email = credentials.email as string | undefined;
const password = credentials.password as string | undefined;
if (!email || !password) return null;
if (!email || !password) return logAuthReject("email-password", "missing-input", email);
const user = await getUserByEmail(email);
if (!user?.passwordHash) return null;
if (!user) return logAuthReject("email-password", "user-not-found", email);
if (!user.passwordHash) return logAuthReject("email-password", "no-password-hash", email);
const ok = await verifyPassword(password, user.passwordHash);
if (!ok) return null;
if (!ok) return logAuthReject("email-password", "wrong-password", email);
return { id: user.id, email: user.email ?? "", name: user.name ?? "" };
},
}),
@@ -176,13 +200,14 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
async authorize(credentials) {
const userId = credentials.userId as string | undefined;
const password = credentials.password as string | undefined;
if (!userId || !password) return null;
if (!userId || !password) return logAuthReject("user-password", "missing-input", userId);
const user = await getUserById(userId);
if (!user?.passwordHash) return null;
if (!user) return logAuthReject("user-password", "user-not-found", userId);
if (!user.passwordHash) return logAuthReject("user-password", "no-password-hash", userId);
const ok = await verifyPassword(password, user.passwordHash);
if (!ok) return null;
if (!ok) return logAuthReject("user-password", "wrong-password", userId);
return { id: user.id, email: user.email ?? "", name: user.name ?? "" };
},
}),