Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | 6x 6x 6x 6x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 104x 108x 108x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 3x 2x 3x 19x 19x 19x 19x 19x 19x 19x 3x 108x 86x 18x 18x 13x 13x 13x 13x 13x 13x 13x 13x 8x 8x 8x 5x 3x 3x 3x 2x 2x 2x 2x 18x 18x 18x 108x | "use client";
import { useCallback, useEffect, useRef, useState } from "react";
/**
* One EUDI Wallet presentation from the browser's side: start it, show the
* QR, poll the status route until the wallet answers. Shared by the sign-in
* page (`/auth/eudi-qr`) and the link page (`/patient/wallet`, ADR-067); what
* happens once the wallet has answered is the page's own business.
*/
const POLL_MS = 2000;
const TIMEOUT_MS = 5 * 60 * 1000;
/** How long a wallet that opened the request may stay silent before we say so. */
const SILENT_MS = 20 * 1000;
/** What `GET /api/auth/eudi/status` answers. Never a claim of the PID. */
export interface EudiStatusAnswer {
status: string;
error?: string;
walletOpened?: boolean;
/** a sign-in found no account, and this PID may create one (ADR-067) */
canRegister?: boolean;
}
export type EudiPresentationPhase =
| "idle"
| "loading"
| "scanning"
| "done"
| "error"
| "refused"
| "expired";
export interface EudiPresentation {
phase: EudiPresentationPhase;
setPhase: (p: EudiPresentationPhase) => void;
sid: string | null;
qr: string | null;
walletLink: string | null;
walletOpened: boolean;
walletSilent: boolean;
/** the status answer that refused the presentation */
refusal: EudiStatusAnswer | null;
/** the start route's own refusal, when it gave a reason */
startError: string | null;
start: () => Promise<void>;
}
export function useEudiPresentation(opts: {
/** sent to the start route as JSON; none for a plain sign-in */
startBody?: Record<string, unknown>;
/** called once, when the status route answers "completed" or "linked" */
onDone: (answer: EudiStatusAnswer, sid: string) => void | Promise<void>;
}): EudiPresentation {
const [phase, setPhase] = useState<EudiPresentationPhase>("idle");
const [sid, setSid] = useState<string | null>(null);
const [qr, setQr] = useState<string | null>(null);
const [walletLink, setWalletLink] = useState<string | null>(null);
const [walletOpened, setWalletOpened] = useState(false);
const [walletSilent, setWalletSilent] = useState(false);
const [refusal, setRefusal] = useState<EudiStatusAnswer | null>(null);
const [startError, setStartError] = useState<string | null>(null);
const sidRef = useRef<string | null>(null);
const startedAtRef = useRef<number>(0);
const openedAtRef = useRef<number>(0);
const onDoneRef = useRef(opts.onDone);
useEffect(() => {
onDoneRef.current = opts.onDone;
});
const bodyJson = opts.startBody ? JSON.stringify(opts.startBody) : null;
const start = useCallback(async () => {
setPhase("loading");
setQr(null);
setWalletLink(null);
setWalletOpened(false);
setWalletSilent(false);
setRefusal(null);
setStartError(null);
openedAtRef.current = 0;
sidRef.current = null;
setSid(null);
try {
const res = await fetch(
"/api/auth/eudi/start",
bodyJson
? {
method: "POST",
headers: { "Content-Type": "application/json" },
body: bodyJson,
}
: { method: "POST" },
);
if (!res.ok) {
const data = (await res.json().catch(() => ({}))) as {
error?: string;
};
setStartError(typeof data.error === "string" ? data.error : null);
throw new Error("start failed");
}
const data = (await res.json()) as {
sid: string;
qrDataUri: string;
walletLink: string;
};
sidRef.current = data.sid;
setSid(data.sid);
setQr(data.qrDataUri);
setWalletLink(data.walletLink);
startedAtRef.current = Date.now();
setPhase("scanning");
} catch {
setPhase("error");
}
}, [bodyJson]);
// Poll the verifier (via our server) until the wallet answers.
useEffect(() => {
if (phase !== "scanning") return;
let active = true;
const id = setInterval(async () => {
const current = sidRef.current;
Iif (!current) return;
Iif (Date.now() - startedAtRef.current > TIMEOUT_MS) {
if (active) setPhase("expired");
return;
}
try {
const res = await fetch(
`/api/auth/eudi/status?sid=${encodeURIComponent(current)}`,
);
Iif (res.status === 404) {
if (active) setPhase("expired");
return;
}
const data = (await res.json()) as EudiStatusAnswer;
// A PID that opens nothing here, a declined wallet, a PID another
// account holds (#708, ADR-067).
if ((res.status === 403 || res.status === 409) && active) {
setRefusal({
...data,
error: data.error ?? "The wallet could not sign you in.",
});
setPhase("refused");
return;
}
if (
(data.status === "completed" || data.status === "linked") &&
active
) {
setPhase("done");
await onDoneRef.current(data, current);
return;
}
// The wallet fetched the request but has not answered (#708).
Eif (data.walletOpened && active) {
if (!openedAtRef.current) openedAtRef.current = Date.now();
setWalletOpened(true);
setWalletSilent(Date.now() - openedAtRef.current > SILENT_MS);
}
} catch {
/* transient: keep polling */
}
}, POLL_MS);
return () => {
active = false;
clearInterval(id);
};
}, [phase]);
return {
phase,
setPhase,
sid,
qr,
walletLink,
walletOpened,
walletSilent,
refusal,
startError,
start,
};
}
|