Login page and Settings → eGov: relay session with a server-side challenge/nonce, QR + eGov Mobile deep link, polling, one-shot completion; IIN from the signer's certificate (own DER reader), users.egov_iin. The relay is trusted (PM decision): no own XMLDSig check. CLI peppa user egov, mock-kazeds-relay, tests. Dev mode: /_next/static is no-store (Cloudflare cached stale chunks). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
77 lines
3.8 KiB
TypeScript
77 lines
3.8 KiB
TypeScript
"use client";
|
||
|
||
import { useEffect, useRef, useState } from "react";
|
||
import { QrCode, Smartphone } from "lucide-react";
|
||
import { Button } from "@/components/ui/button";
|
||
|
||
type Phase = { at: "idle" } | { at: "qr"; qr: string; deeplink: string; status: string } | { at: "done" } | { at: "error"; message: string };
|
||
|
||
const STATUS: Record<string, string> = {
|
||
pending: "Отсканируйте QR в eGov Mobile",
|
||
scanned: "QR отсканирован — подпишите в eGov Mobile",
|
||
completed: "Подписано, входим…",
|
||
};
|
||
|
||
/**
|
||
* eGov QR (PEP-65): shows the relay's QR and a deep link for the same phone, polls the flow and
|
||
* finishes it. `login` signs in; `link` binds the IIN to the current account (Settings).
|
||
*/
|
||
export function EgovSign({ purpose, next, label }: { purpose: "login" | "link"; next?: string; label: string }) {
|
||
const [phase, setPhase] = useState<Phase>({ at: "idle" });
|
||
const alive = useRef(true);
|
||
// Set on mount too: StrictMode (dev) mounts, unmounts and mounts again.
|
||
useEffect(() => {
|
||
alive.current = true;
|
||
return () => void (alive.current = false);
|
||
}, []);
|
||
|
||
const fail = (message: string) => alive.current && setPhase({ at: "error", message });
|
||
|
||
async function start() {
|
||
const r = await fetch("/auth/egov/start", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ purpose }) });
|
||
const body = (await r.json()) as { qr?: string; deeplink?: string; error?: string };
|
||
if (!r.ok || !body.qr) return fail(body.error ?? "Не удалось создать QR");
|
||
setPhase({ at: "qr", qr: body.qr, deeplink: body.deeplink ?? "", status: "pending" });
|
||
for (;;) {
|
||
await new Promise((res) => setTimeout(res, 2000));
|
||
const s = (await (await fetch("/auth/egov/status", { cache: "no-store" })).json().catch(() => ({}))) as { status?: string; error?: string };
|
||
if (!alive.current) return;
|
||
if (s.error) return fail(s.error);
|
||
if (s.status === "expired") return fail("QR-код истёк — начните заново");
|
||
if (s.status === "rejected") return fail("Подписание отменено в eGov Mobile");
|
||
setPhase((p) => (p.at === "qr" ? { ...p, status: s.status ?? p.status } : p));
|
||
if (s.status === "completed") break;
|
||
}
|
||
const c = await fetch("/auth/egov/complete", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ next }) });
|
||
const done = (await c.json().catch(() => ({}))) as { redirect?: string; error?: string };
|
||
if (!c.ok || !done.redirect) return fail(done.error ?? "Не удалось войти через eGov");
|
||
setPhase({ at: "done" });
|
||
window.location.assign(done.redirect);
|
||
}
|
||
|
||
if (phase.at === "qr") {
|
||
return (
|
||
<div className="flex w-full flex-col items-center gap-3 rounded-lg border bg-card p-4">
|
||
{/* eslint-disable-next-line @next/next/no-img-element -- data URL from the server */}
|
||
<img src={phase.qr} alt="QR для eGov Mobile" width={220} height={220} className="rounded-md bg-white p-1" />
|
||
<p className="m-0 text-sm text-muted-foreground" aria-live="polite">{STATUS[phase.status] ?? phase.status}</p>
|
||
{phase.deeplink && (
|
||
<Button asChild variant="outline" size="sm" className="w-full">
|
||
<a href={phase.deeplink}>
|
||
<Smartphone /> Открыть в eGov Mobile
|
||
</a>
|
||
</Button>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|
||
return (
|
||
<div className="flex w-full flex-col gap-2">
|
||
{phase.at === "error" && <p className="m-0 text-sm text-destructive">{phase.message}</p>}
|
||
<Button variant="outline" size="lg" className="w-full" disabled={phase.at === "done"} onClick={() => void start().catch((e: Error) => fail(e.message))}>
|
||
<QrCode /> {phase.at === "error" ? "Новый QR" : label}
|
||
</Button>
|
||
</div>
|
||
);
|
||
}
|