Improve registration logging and error handling

Adds detailed logging for registration attempts (excluding passwords) and Turnstile verification failures. Enhances error handling by distinguishing between validation, business logic, and server errors, returning appropriate HTTP status codes.
This commit is contained in:
Christopher
2026-01-26 23:08:12 -08:00
parent 75a2d87d9a
commit 9739539733
+21 -3
View File
@@ -10,12 +10,17 @@ const registerSchema = z.object({
email: z.string().email(),
password: z.string().min(8),
displayName: z.string().optional(),
turnstileToken: z.string().optional(),
turnstileToken: z.string().nullable().optional(),
});
export async function POST(request: Request) {
try {
const body = await request.json();
// Log registration attempt (excluding password)
const { password, ...logData } = body;
console.log('Registration attempt details:', logData);
const data = registerSchema.parse(body);
// Verify Turnstile token if provided
@@ -23,6 +28,7 @@ export async function POST(request: Request) {
const ip = request.headers.get('x-forwarded-for') || request.headers.get('x-real-ip') || undefined;
const isValid = await verifyTurnstileToken(data.turnstileToken, ip);
if (!isValid) {
console.error('Turnstile verification failed for handle:', data.handle);
return NextResponse.json(
{ error: 'Bot verification failed. Please try again.' },
{ status: 400 }
@@ -65,18 +71,30 @@ export async function POST(request: Request) {
},
});
} catch (error) {
console.error('Registration error:', error);
console.error('Registration error detailed:', error);
if (error instanceof z.ZodError) {
console.error('Validation error:', error.issues);
return NextResponse.json(
{ error: 'Invalid input', details: error.issues },
{ status: 400 }
);
}
const errorMessage = error instanceof Error ? error.message : 'Registration failed';
// Return 400 for known business logic errors
if (errorMessage.includes('taken') || errorMessage.includes('registered') || errorMessage.includes('Handle must be')) {
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Registration failed' },
{ error: errorMessage },
{ status: 400 }
);
}
// Return 500 for everything else so we can see it's a server error
return NextResponse.json(
{ error: `Server error: ${errorMessage}` },
{ status: 500 }
);
}
}