mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
fix(auth): say why a registration or passkey sign-in was refused
Once the sign-in and registration pages read codes instead of the server's prose, a taken username or address said only "Registration failed", an expired invitation said "You don't have permission for this", and a passkey this server does not know, a passkey sign-in that took too long, an unreadable passkey answer, a single-sign-on-only server and an already signed-in visitor all said "Login failed". The registration route now names its refusals (auth.register.taken for both 409s, auth.register.invite_invalid for a refused invitation), and those codes, the three passkey refusals, oidc_only and auth.already_authenticated get a sentence in every language.
This commit is contained in:
1 parent
94ea48d723
commit
37734c9fd8
16 files changed
+253
-29
No files matched your search
@@ -226,8 +226,13 @@ describe("POST /api/auth/register — first-admin atomicity (M1)", () => {
|
||||
|
||||
const res = await postPathThrough(validBody);
|
||||
expect(res.status).toBe(409);
|
||||
const body = (await res.json()) as { error: string };
|
||||
const body = (await res.json()) as {
|
||||
error: string;
|
||||
meta?: { errorCode?: string };
|
||||
};
|
||||
expect(body.error).toBe("Username or email already taken");
|
||||
// The code the registration page turns into "already in use".
|
||||
expect(body.meta?.errorCode).toBe("auth.register.taken");
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -144,7 +144,9 @@ export const POST = apiHandler(async (request: NextRequest) => {
|
||||
prisma.user.findUnique({ where: { username } }),
|
||||
]);
|
||||
if (existingEmail || existingUsername) {
|
||||
return apiError("Username or email already taken", 409);
|
||||
return apiError("Username or email already taken", 409, {
|
||||
errorCode: "auth.register.taken",
|
||||
});
|
||||
}
|
||||
|
||||
// Validate password strength (locale-aware feedback)
|
||||
@@ -196,7 +198,9 @@ export const POST = apiHandler(async (request: NextRequest) => {
|
||||
},
|
||||
});
|
||||
if (!registrationEnabled) {
|
||||
return apiError("Invalid or expired invite", 403);
|
||||
return apiError("Invalid or expired invite", 403, {
|
||||
errorCode: "auth.register.invite_invalid",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -249,7 +253,9 @@ export const POST = apiHandler(async (request: NextRequest) => {
|
||||
});
|
||||
|
||||
if (user === null) {
|
||||
return apiError("Username or email already taken", 409);
|
||||
return apiError("Username or email already taken", 409, {
|
||||
errorCode: "auth.register.taken",
|
||||
});
|
||||
}
|
||||
|
||||
// Create session immediately. v1.4.22 W5 reconcile (Sr-H1) —
|
||||
|
||||
@@ -125,14 +125,39 @@ describe("registration refusals", () => {
|
||||
return screen;
|
||||
}
|
||||
|
||||
it("says the registration failed instead of the server's words", async () => {
|
||||
it("says what to change when the name or address is taken", async () => {
|
||||
const screen = await register({
|
||||
status: 409,
|
||||
error: "Username or email already taken",
|
||||
meta: { errorCode: "http.conflict" },
|
||||
meta: { errorCode: "auth.register.taken" },
|
||||
});
|
||||
await expect
|
||||
.element(
|
||||
screen.getByText("That username or email address is already in use."),
|
||||
)
|
||||
.toBeVisible();
|
||||
expect(document.body.textContent).not.toContain("already taken");
|
||||
});
|
||||
|
||||
it("tells an invitee their link no longer works", async () => {
|
||||
const screen = await register({
|
||||
status: 403,
|
||||
error: "Invalid or expired invite",
|
||||
meta: { errorCode: "auth.register.invite_invalid" },
|
||||
});
|
||||
await expect
|
||||
.element(screen.getByText(/This invitation is no longer valid/))
|
||||
.toBeVisible();
|
||||
});
|
||||
|
||||
it("says the registration failed for a refusal it has no words for", async () => {
|
||||
const screen = await register({
|
||||
status: 500,
|
||||
error: "Internal error",
|
||||
meta: { errorCode: "http.internal" },
|
||||
});
|
||||
await expect.element(screen.getByText("Registration failed")).toBeVisible();
|
||||
expect(document.body.textContent).not.toContain("already taken");
|
||||
expect(document.body.textContent).not.toContain("Internal error");
|
||||
});
|
||||
|
||||
it("shows the password feedback, which the server words for the reader", async () => {
|
||||
|
||||
Reference in new issue
Block a user