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 | 5x 5x 5x 5x 86x 86x 86x 86x 86x 2x 86x 86x 86x 4x 4x 4x 4x 4x 4x 4x 1x 1x 3x 4x 86x 3x 3x 3x 3x 86x 6x 6x 6x 2x 2x 4x 4x 4x 86x | "use client";
import { useCallback, useState } from "react";
import { signIn } from "next-auth/react";
import {
useEudiPresentation,
type EudiPresentation,
} from "@/lib/use-eudi-presentation";
/**
* Signing in with the citizen wallet, as the sign-in page (`/auth/eudi-qr`)
* and the start page's register dialog both do it. Two ways in:
*
* - **The QR code**, a real OpenID4VP request from the hub's verifier. A PID
* on an account signs in to it; a PID on no account may create one
* (ADR-067), and the page shows the new login once before signing in.
* - **The simulated phone** next to it, for a visitor without the wallet.
* Approving it asks the server for a demo transaction (only where
* `EUDI_DEMO_WALLET=true`) and signs in as the demo patient `patient1`
* through the same `eudi-wallet` provider, at assurance level low.
*
* Both end in the `eudi-wallet` Credentials provider of `lib/auth.ts`, so the
* visitor holds a real session and lands on a page that needs one.
*/
/** Messages kept in state as their English key and translated when shown. */
const HUB_COULD_NOT_CREATE = "The hub could not create the account.";
const SIMULATION_ONLY =
"Simulation only on this deployment. Scan the QR code with {wallet} to sign in.";
/**
* Where a new account lands when its owner chose to connect Klarbefund next:
* the card of ADR-049 on the profile page, the one way the app connects to
* an account the website created.
*/
const CONNECT_APP_PATH = "/patient/profile#connect-app";
/** The account a PID created (ADR-067), shown once and then never again. */
export interface NewAccount {
username: string;
password: string;
}
export interface WalletSignIn {
presentation: EudiPresentation;
account: NewAccount | null;
registering: boolean;
registerError: string | null;
/** set when the deployment refuses the simulated phone */
demoHint: string | null;
/** creates the account for a PID the status route refused with `canRegister` */
register: () => Promise<void>;
/** signs in to the new account, on to Klarbefund when the owner chose it */
continueSignedIn: (opts?: { connectApp?: boolean }) => Promise<void>;
/** the simulated phone's approval: signs in as the demo patient */
approveSimulated: () => Promise<void>;
restart: () => void;
}
export function useWalletSignIn({
callbackUrl,
}: {
/** where a sign-in lands */
callbackUrl: string;
}): WalletSignIn {
const [account, setAccount] = useState<NewAccount | null>(null);
const [registering, setRegistering] = useState(false);
const [registerError, setRegisterError] = useState<string | null>(null);
const [demoHint, setDemoHint] = useState<string | null>(null);
const presentation = useEudiPresentation({
onDone: async (_answer, sid) => {
await signIn("eudi-wallet", { sid, callbackUrl });
},
});
const { start, sid, setPhase } = presentation;
const restart = useCallback(() => {
setRegisterError(null);
void start();
}, [start]);
// ADR-067: a PID on no account may create one. The password is answered
// once; the page shows it until the person goes on, then signs in.
const register = useCallback(async () => {
Iif (!sid) return;
setRegistering(true);
setRegisterError(null);
try {
const res = await fetch("/api/auth/eudi/register", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ sid }),
});
const data = (await res.json().catch(() => ({}))) as {
username?: string;
password?: string;
error?: string;
reason?: string;
};
if (!res.ok || !data.username || !data.password) {
setRegisterError(data.reason ?? data.error ?? HUB_COULD_NOT_CREATE);
return;
}
setAccount({ username: data.username, password: data.password });
} catch {
setRegisterError(HUB_COULD_NOT_CREATE);
} finally {
setRegistering(false);
}
}, [sid]);
const continueSignedIn = useCallback(
async (opts?: { connectApp?: boolean }) => {
Iif (!sid) return;
setAccount(null);
setPhase("done");
await signIn("eudi-wallet", {
sid,
callbackUrl: opts?.connectApp ? CONNECT_APP_PATH : callbackUrl,
});
},
[sid, callbackUrl, setPhase],
);
// Where the deployment refuses the demo transaction, the phone stays a
// picture and says so.
const approveSimulated = useCallback(async () => {
try {
const res = await fetch("/api/auth/eudi/start", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ demo: true }),
});
if (!res.ok) {
setDemoHint(SIMULATION_ONLY);
return;
}
const data = (await res.json()) as { sid: string };
setPhase("done");
await signIn("eudi-wallet", { sid: data.sid, callbackUrl });
} catch {
setDemoHint(SIMULATION_ONLY);
}
}, [callbackUrl, setPhase]);
return {
presentation,
account,
registering,
registerError,
demoHint,
register,
continueSignedIn,
approveSimulated,
restart,
};
}
|