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:
parent
309b77e63c
commit
a4cae66336
9 changed files with 528 additions and 28 deletions
50
src/api.ts
50
src/api.ts
|
|
@ -8,12 +8,14 @@ export type Session =
|
|||
| { authenticated: false }
|
||||
| { authenticated: true; user: User; csrf_token: string };
|
||||
export interface HubInfo {
|
||||
capabilities: { github_auth: string };
|
||||
capabilities: { github_auth: string; email_auth?: string };
|
||||
}
|
||||
export interface RequestOptions {
|
||||
signal?: AbortSignal;
|
||||
method?: "GET" | "POST";
|
||||
csrf?: string;
|
||||
/** JSON request body; only sent when given. */
|
||||
body?: unknown;
|
||||
fetcher?: (input: string, options: RequestInit) => Promise<Response>;
|
||||
}
|
||||
export class ApiError extends Error {
|
||||
|
|
@ -26,7 +28,7 @@ export class ApiError extends Error {
|
|||
}
|
||||
export async function request(
|
||||
path: string,
|
||||
{ signal, method = "GET", csrf, fetcher = fetch }: RequestOptions = {},
|
||||
{ signal, method = "GET", csrf, body, fetcher = fetch }: RequestOptions = {},
|
||||
): Promise<unknown> {
|
||||
if (!path.startsWith("/v1/"))
|
||||
throw new TypeError("Only same-origin VNOX API paths are allowed");
|
||||
|
|
@ -38,7 +40,9 @@ export async function request(
|
|||
headers: {
|
||||
Accept: "application/json",
|
||||
...(csrf ? { "X-CSRF-Token": csrf } : {}),
|
||||
...(body === undefined ? {} : { "Content-Type": "application/json" }),
|
||||
},
|
||||
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
||||
});
|
||||
if (!response.ok) throw new ApiError(response.status, "request_failed");
|
||||
if (response.status === 204) return null;
|
||||
|
|
@ -70,9 +74,49 @@ export function isInfo(value: unknown): value is HubInfo {
|
|||
return (
|
||||
record(value) &&
|
||||
record(value.capabilities) &&
|
||||
typeof value.capabilities.github_auth === "string"
|
||||
typeof value.capabilities.github_auth === "string" &&
|
||||
(value.capabilities.email_auth === undefined ||
|
||||
typeof value.capabilities.email_auth === "string")
|
||||
);
|
||||
}
|
||||
/** Result of asking Hub to mail a sign-in code. */
|
||||
export interface CodeRequest {
|
||||
resendAfter: number;
|
||||
}
|
||||
/** Asks Hub to email a one-time code. Resolves with a bounded resend delay in seconds. */
|
||||
export async function requestEmailCode(
|
||||
email: string,
|
||||
lang: "ru" | "en",
|
||||
options: Pick<RequestOptions, "signal" | "fetcher"> = {},
|
||||
): Promise<CodeRequest> {
|
||||
const value = await request("/v1/auth/email/start", {
|
||||
...options,
|
||||
method: "POST",
|
||||
body: { email, lang },
|
||||
});
|
||||
const delay = record(value) ? value.resend_after : undefined;
|
||||
return {
|
||||
resendAfter:
|
||||
typeof delay === "number" && delay >= 1 && delay <= 3600
|
||||
? Math.ceil(delay)
|
||||
: 60,
|
||||
};
|
||||
}
|
||||
/** Redeems the code. Resolves only with an authenticated session. */
|
||||
export async function verifyEmailCode(
|
||||
email: string,
|
||||
code: string,
|
||||
options: Pick<RequestOptions, "signal" | "fetcher"> = {},
|
||||
): Promise<Session> {
|
||||
const value = await request("/v1/auth/email/verify", {
|
||||
...options,
|
||||
method: "POST",
|
||||
body: { email, code },
|
||||
});
|
||||
if (!isSession(value) || !value.authenticated)
|
||||
throw new Error("invalid_session");
|
||||
return value;
|
||||
}
|
||||
export function safeAvatar(value: unknown): string | null {
|
||||
if (typeof value !== "string") return null;
|
||||
try {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue