62 lines
2.2 KiB
TypeScript
62 lines
2.2 KiB
TypeScript
/**
|
|
* AK-P14b (P14 email marketing) — public double-opt-in confirmation route.
|
|
*
|
|
* The opt-in mail carries `/optin/<token>`. Verifies the HMAC marketing token
|
|
* (purpose "optin") and flips the contact's KontaktMarketingConsent to `double`.
|
|
* Idempotent: a second click is a no-op. No session / CSRF — the token IS the
|
|
* auth (same stance as the unsubscribe route).
|
|
*/
|
|
|
|
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
|
|
import { useLoaderData } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import { confirmOptIn } from "~/modules/emailMarketing/services/ConsentService.server";
|
|
import { createScopedLogger } from "~/utils/logger.server";
|
|
|
|
const logger = createScopedLogger("marketing.optin");
|
|
|
|
type LoaderData = {
|
|
status: "confirmed" | "already_confirmed" | "invalid";
|
|
};
|
|
|
|
async function processOptIn(token: string): Promise<LoaderData> {
|
|
const result = await confirmOptIn(token);
|
|
logger.info({ op: "optin.processed", result }, "double opt-in processed");
|
|
return { status: result };
|
|
}
|
|
|
|
export async function loader({ params }: LoaderFunctionArgs): Promise<LoaderData> {
|
|
return processOptIn(params.token ?? "");
|
|
}
|
|
|
|
export async function action({ params }: ActionFunctionArgs): Promise<LoaderData> {
|
|
return processOptIn(params.token ?? "");
|
|
}
|
|
|
|
const MESSAGES: Record<LoaderData["status"], { title: string; description: string }> = {
|
|
confirmed: {
|
|
title: "marketing.optin.confirmed.title",
|
|
description: "marketing.optin.confirmed.description",
|
|
},
|
|
already_confirmed: {
|
|
title: "marketing.optin.alreadyConfirmed.title",
|
|
description: "marketing.optin.alreadyConfirmed.description",
|
|
},
|
|
invalid: {
|
|
title: "marketing.optin.invalid.title",
|
|
description: "marketing.optin.invalid.description",
|
|
},
|
|
};
|
|
|
|
export default function OptInRoute() {
|
|
const data = useLoaderData<LoaderData>();
|
|
const { t } = useTranslation();
|
|
const msg = MESSAGES[data.status];
|
|
return (
|
|
<main style={{ maxWidth: "600px", margin: "60px auto", padding: "0 20px", fontFamily: "system-ui, sans-serif" }}>
|
|
<h1 style={{ fontSize: "24px" }}>{t(msg.title)}</h1>
|
|
<p style={{ fontSize: "16px", lineHeight: "1.5", color: "#475569" }}>{t(msg.description)}</p>
|
|
</main>
|
|
);
|
|
}
|