feat(web): sign in by emailed code on the account page

Adds the email form (address, code, resend timer, aria-live status) shown only when
/v1/info reports email_auth enabled, RU/EN strings, privacy policy update, API
helpers, tests and browser QA with a mocked Hub.
This commit is contained in:
Loki 2026-10-24 17:49:41 +02:00
parent 309b77e63c
commit a4cae66336
Signed by: boba
GPG key ID: 253067914055423B
9 changed files with 528 additions and 28 deletions

View file

@ -1,6 +1,14 @@
import { test } from "bun:test";
import assert from "node:assert/strict";
import { request, isSession, isInfo, safeAvatar, ApiError } from "../src/api";
import {
request,
isSession,
isInfo,
safeAvatar,
ApiError,
requestEmailCode,
verifyEmailCode,
} from "../src/api";
import { messages, detectLanguage } from "../src/i18n";
test("catalogs match and never silently fall back to a missing translation", () => {
@ -105,3 +113,63 @@ test("Hub capabilities reject malformed JSON before enabling sign-in", () => {
assert.equal(isInfo(value), false);
}
});
test("email_auth capability must be a string when present", () => {
assert.ok(
isInfo({ capabilities: { github_auth: "enabled", email_auth: "enabled" } }),
);
assert.ok(isInfo({ capabilities: { github_auth: "enabled" } }));
assert.equal(
isInfo({ capabilities: { github_auth: "enabled", email_auth: true } }),
false,
);
});
test("email code requests send JSON same-origin and bound the resend delay", async () => {
let seen: { path: string; init: RequestInit } | undefined;
const result = await requestEmailCode("a@example.com", "en", {
fetcher: async (path, init) => {
seen = { path, init };
return Response.json({ resend_after: 999999 }, { status: 202 });
},
});
assert.equal(result.resendAfter, 60);
assert.ok(seen);
assert.equal(seen.path, "/v1/auth/email/start");
assert.equal(seen.init.method, "POST");
assert.equal(seen.init.credentials, "same-origin");
assert.equal(
new Headers(seen.init.headers).get("Content-Type"),
"application/json",
);
assert.deepEqual(JSON.parse(String(seen.init.body)), {
email: "a@example.com",
lang: "en",
});
const short = await requestEmailCode("a@example.com", "ru", {
fetcher: async () => Response.json({ resend_after: 30 }, { status: 202 }),
});
assert.equal(short.resendAfter, 30);
});
test("email verification accepts only an authenticated session and keeps the status", async () => {
const session = {
authenticated: true,
csrf_token: "fixture",
user: { user_id: "u", handle: "h", display_name: "n" },
};
const ok = await verifyEmailCode("a@example.com", "12345678", {
fetcher: async () => Response.json(session),
});
assert.equal(ok.authenticated, true);
await assert.rejects(
verifyEmailCode("a@example.com", "12345678", {
fetcher: async () => Response.json({ authenticated: false }),
}),
);
await assert.rejects(
verifyEmailCode("a@example.com", "12345678", {
fetcher: async () =>
Response.json({ error: "invalid_code" }, { status: 400 }),
}),
(error: unknown) => error instanceof ApiError && error.status === 400,
);
});