Make registration errors informative

- Backend: explicitly return 409 EMAIL_TAKEN when the email already
  exists, instead of Better Auth's silent 200 (common after migration).
- Frontend: split error handling — network failure, non-JSON body
  (502/proxy), and meaningful server errors get distinct messages; the
  "email already registered" case shows login / reset-password links
  instead of a generic "connection error".

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-12 13:20:56 +05:00
parent 05f46ec830
commit 8240db5d22
2 changed files with 71 additions and 19 deletions
+15 -2
View File
@@ -1,5 +1,6 @@
import { NextRequest } from "next/server";
import { auth } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
// eslint-disable-next-line @typescript-eslint/no-require-imports
const disposableDomains = require("disposable-email-domains") as string[];
@@ -21,8 +22,8 @@ async function verifyTurnstile(token: string): Promise<boolean> {
}
}
function jsonError(message: string, status: number) {
return new Response(JSON.stringify({ message }), {
function jsonError(message: string, status: number, code?: string) {
return new Response(JSON.stringify({ message, code }), {
status,
headers: { "Content-Type": "application/json" },
});
@@ -59,6 +60,18 @@ export async function POST(request: NextRequest) {
}
}
// Email уже зарегистрирован? Сообщаем явно, а не молчаливым «успехом»
// от Better Auth — частый случай после миграции (у людей уже есть аккаунт).
if (email) {
const existing = await prisma.user.findFirst({
where: { email: { equals: email, mode: "insensitive" } },
select: { id: true },
});
if (existing) {
return jsonError("Аккаунт с таким email уже зарегистрирован", 409, "EMAIL_TAKEN");
}
}
const baseUrl =
process.env.BETTER_AUTH_URL ??
`https://${request.headers.get("host")}`;