91 lines
3.2 KiB
TypeScript
91 lines
3.2 KiB
TypeScript
import { useActionData, useLoaderData, useNavigation, type ActionFunctionArgs, type LoaderFunctionArgs } from "react-router";
|
|
import { CatalogError } from "@hivegrid/foundation";
|
|
import { recordSignatureLinkOpened, signWithOtp, SignatureServiceError } from "~/modules/signature/services/signatureService.server";
|
|
import { getTrustedClientIp } from "~/utils/server/IpUtils";
|
|
import { PublicSignPage } from "~/components/screens/signature/PublicSignPage";
|
|
|
|
type LoaderData = {
|
|
title: string;
|
|
signerName: string;
|
|
signerEmail: string;
|
|
status: string;
|
|
};
|
|
|
|
type ActionData = { ok: true } | { ok: false; error: string };
|
|
|
|
function requestIp(request: Request): string | null {
|
|
const ip = getTrustedClientIp(request);
|
|
return ip === "unknown" ? null : ip;
|
|
}
|
|
|
|
function userAgent(request: Request): string | null {
|
|
return request.headers.get("user-agent");
|
|
}
|
|
|
|
function formString(formData: FormData, name: string): string | null {
|
|
const value = formData.get(name);
|
|
return typeof value === "string" ? value : null;
|
|
}
|
|
|
|
export async function loader({ request, params }: LoaderFunctionArgs) {
|
|
const token = params.token;
|
|
if (!token) throw new Response(null, { status: 404 });
|
|
try {
|
|
const data = await recordSignatureLinkOpened({ token, ipAddress: requestIp(request), userAgent: userAgent(request) });
|
|
return Response.json({ title: data.title, signerName: data.signerName, signerEmail: data.signerEmail, status: data.status } satisfies LoaderData);
|
|
} catch (error) {
|
|
if (error instanceof CatalogError) throw new Response(null, { status: 404 });
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export async function action({ request, params }: ActionFunctionArgs) {
|
|
const token = params.token;
|
|
if (!token) throw new Response(null, { status: 404 });
|
|
const formData = await request.formData();
|
|
try {
|
|
await signWithOtp({
|
|
token,
|
|
otp: formString(formData, "otp"),
|
|
signatureSvg: formString(formData, "signatureSvg"),
|
|
intentAccepted: formData.get("intentAccepted") === "on",
|
|
ipAddress: requestIp(request),
|
|
userAgent: userAgent(request),
|
|
trustedClientIp: getTrustedClientIp(request),
|
|
});
|
|
return Response.json({ ok: true } satisfies ActionData);
|
|
} catch (error) {
|
|
if (error instanceof SignatureServiceError) {
|
|
const headers = error.retryAfterSeconds ? { "Retry-After": String(error.retryAfterSeconds) } : undefined;
|
|
return Response.json({ ok: false, error: error.messageKey } satisfies ActionData, { status: error.status, headers });
|
|
}
|
|
if (error instanceof CatalogError) throw new Response(null, { status: 404 });
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export function headers() {
|
|
return {
|
|
"Cache-Control": "no-store",
|
|
"Referrer-Policy": "no-referrer",
|
|
};
|
|
}
|
|
|
|
export default function SignatureLinkRoute() {
|
|
const data = useLoaderData<LoaderData>();
|
|
const actionData = useActionData<ActionData>();
|
|
const navigation = useNavigation();
|
|
const isComplete = actionData?.ok === true || data.status === "signed";
|
|
|
|
return (
|
|
<PublicSignPage
|
|
documentTitle={data.title}
|
|
signerName={data.signerName}
|
|
signerEmail={data.signerEmail}
|
|
isComplete={isComplete}
|
|
errorKey={actionData?.ok === false ? actionData.error : null}
|
|
submitting={navigation.state !== "idle"}
|
|
/>
|
|
);
|
|
}
|