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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 1x 7x 3x 7x 7x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 7x 7x 3x | "use client";
import { signIn } from "next-auth/react";
import { useRouter, useSearchParams } from "next/navigation";
import { Suspense, useCallback, useEffect, useRef, useState } from "react";
import {
ShieldCheck,
RefreshCw,
CheckCircle2,
AlertTriangle,
} from "lucide-react";
import { WalletFlow } from "@/components/wallet/PhoneFrame";
import { REGISTER_STEPS, LOGIN_STEPS } from "@/components/wallet/flows";
import { EudiApprovalFlow } from "@/components/wallet/EudiApprovalFlow";
import { setDemoPersona } from "@/lib/use-demo-persona";
const POLL_MS = 2000;
const TIMEOUT_MS = 5 * 60 * 1000;
type Phase =
| "loading"
| "scanning"
| "completed"
| "error"
| "expired"
| "unavailable";
function EudiQrContent() {
// Read at render-time (not module load) so it is deterministic in tests and
// immune to test-ordering env pollution; NEXT_PUBLIC_* is build-inlined anyway.
const IS_STATIC = process.env.NEXT_PUBLIC_STATIC_EXPORT === "true";
const searchParams = useSearchParams();
const router = useRouter();
const callbackUrl = searchParams.get("callbackUrl") || "/patient";
const mode = searchParams.get("mode") === "login" ? "login" : "register";
const [phase, setPhase] = useState<Phase>(
IS_STATIC ? "unavailable" : "loading",
);
const [qr, setQr] = useState<string | null>(null);
const [walletLink, setWalletLink] = useState<string | null>(null);
const sidRef = useRef<string | null>(null);
const startedAtRef = useRef<number>(0);
const start = useCallback(async () => {
Iif (IS_STATIC) {
setPhase("unavailable");
return;
}
setPhase("loading");
setQr(null);
setWalletLink(null);
sidRef.current = null;
try {
const res = await fetch("/api/auth/eudi/start", { method: "POST" });
if (!res.ok) throw new Error("start failed");
const data = (await res.json()) as {
sid: string;
qrDataUri: string;
walletLink: string;
};
sidRef.current = data.sid;
setQr(data.qrDataUri);
setWalletLink(data.walletLink);
startedAtRef.current = Date.now();
setPhase("scanning");
} catch {
setPhase("error");
}
}, [IS_STATIC]);
useEffect(() => {
start();
}, [start]);
// Poll the verifier (via our server) until the wallet completes the presentation.
useEffect(() => {
if (phase !== "scanning") return;
let active = true;
const id = setInterval(async () => {
const sid = sidRef.current;
Iif (!sid) return;
Iif (Date.now() - startedAtRef.current > TIMEOUT_MS) {
if (active) setPhase("expired");
return;
}
try {
const res = await fetch(
`/api/auth/eudi/status?sid=${encodeURIComponent(sid)}`,
);
Iif (res.status === 404) {
if (active) setPhase("expired");
return;
}
const data = (await res.json()) as { status: string };
Eif (data.status === "completed" && active) {
setPhase("completed");
await signIn("eudi-wallet", { sid, callbackUrl });
}
} catch {
/* transient — keep polling */
}
}, POLL_MS);
return () => {
active = false;
clearInterval(id);
};
}, [phase, callbackUrl]);
// Static export / demo: no live verifier backend, so drive an interactive
// QR + wallet-approval simulation; on approval, sign in as the demo patient
// and forward to their personal health record (the "EHR page").
Iif (IS_STATIC) {
const onComplete = () => {
setDemoPersona("patient1");
try {
localStorage.setItem("demo-persona", "patient1");
} catch {
/* storage unavailable — non-fatal for the visual demo */
}
router.push(callbackUrl);
};
return (
<div className="min-h-[60vh] flex flex-col items-center justify-center gap-6 py-10 px-4">
<div className="text-center max-w-md">
<ShieldCheck
size={40}
className="mx-auto mb-2 text-blue-800 dark:text-blue-300"
/>
<h1 className="text-2xl font-bold text-[var(--text-primary)] mb-1">
{mode === "login" ? "Sign in" : "Register"} with your EUDI Wallet
</h1>
<p className="text-[var(--text-secondary)] text-sm">
Scan the QR with your EU Digital Identity Wallet, or approve on the
simulated phone (OpenID4VP).
<br />
<span className="text-xs">
Synthetic demo · maps to a demo patient
</span>
</p>
</div>
<EudiApprovalFlow
mode={mode}
onComplete={onComplete}
onCancel={() => router.push("/")}
/>
<a
href="/auth/signin"
className="text-xs text-[var(--text-secondary)] hover:text-[var(--accent)] underline"
>
← Back to other sign-in options
</a>
</div>
);
}
return (
<div className="min-h-[60vh] flex flex-col items-center justify-center gap-6 py-10 px-4">
<div className="flex flex-col lg:flex-row items-center justify-center gap-6 w-full max-w-4xl">
<div className="bg-[var(--surface-2)] rounded-lg p-8 max-w-md w-full text-center">
<ShieldCheck
size={44}
className="mx-auto mb-3 text-blue-800 dark:text-blue-300"
/>
<h1 className="text-2xl font-bold text-[var(--text-primary)] mb-1">
{mode === "login" ? "Sign in" : "Register"} with your EUDI Wallet
</h1>
<p className="text-[var(--text-secondary)] text-sm mb-6">
Scan the QR code with your EU Digital Identity Wallet to verify your
identity (OpenID4VP).
<br />
<span className="text-xs">
Synthetic demo · maps to a demo patient
</span>
</p>
{phase === "loading" && (
<div className="py-10 text-[var(--text-secondary)]">
Preparing a secure request…
</div>
)}
{phase === "scanning" && qr && (
<>
<div className="bg-white rounded-lg p-4 inline-block mb-4">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={qr}
alt="EUDI Wallet OpenID4VP QR code"
width={256}
height={256}
className="block"
/>
</div>
<div className="flex items-center justify-center gap-2 text-sm text-[var(--text-secondary)] mb-3">
<span className="inline-block w-2 h-2 rounded-full bg-green-500 animate-pulse" />
Waiting for your wallet…
</div>
{walletLink && (
<a
href={walletLink}
className="text-xs text-[var(--accent)] underline break-all"
>
On this phone? Tap to open your wallet
</a>
)}
</>
)}
{phase === "completed" && (
<div className="py-8 flex flex-col items-center gap-2 text-green-600 dark:text-green-400">
<CheckCircle2 size={40} />
<p className="font-medium">Verified — signing you in…</p>
</div>
)}
{(phase === "error" || phase === "expired") && (
<div className="py-6">
<AlertTriangle
size={36}
className="mx-auto mb-2 text-amber-500"
/>
<p className="text-sm text-[var(--text-secondary)] mb-4">
{phase === "expired"
? "This sign-in request expired."
: "Could not reach the EUDI verifier."}
</p>
<button
onClick={start}
className="inline-flex items-center gap-2 px-4 py-2 bg-[var(--accent)] hover:bg-[var(--accent-hover)] text-white dark:text-gray-900 rounded-lg font-medium transition-colors"
>
<RefreshCw size={16} /> Try again
</button>
</div>
)}
{phase === "unavailable" && (
<div className="py-8 text-sm text-[var(--text-secondary)]">
EUDI Wallet sign-in is only available on the live deployment, not
in the static demo. Use the demo personas on the{" "}
<a href="/auth/signin" className="text-[var(--accent)] underline">
sign-in page
</a>{" "}
instead.
</div>
)}
</div>
<div className="flex flex-col items-center gap-2 shrink-0">
<WalletFlow
loop
ariaLabel={`Simulated EUDI Wallet ${mode}`}
steps={mode === "login" ? LOGIN_STEPS : REGISTER_STEPS}
/>
<p className="text-xs text-[var(--text-secondary)] max-w-[280px] text-center">
What happens on your phone — a simulated wallet approval
</p>
</div>
</div>
<a
href="/auth/signin"
className="text-xs text-[var(--text-secondary)] hover:text-[var(--accent)] underline"
>
← Back to other sign-in options
</a>
</div>
);
}
export default function EudiQrPage() {
return (
<Suspense
fallback={
<div className="min-h-[60vh] flex items-center justify-center">
<div className="text-[var(--text-secondary)]">Loading…</div>
</div>
}
>
<EudiQrContent />
</Suspense>
);
}
|