276 lines
13 KiB
TypeScript
276 lines
13 KiB
TypeScript
import { useMemo, useState } from "react";
|
|
import { ActionFunction, LoaderFunctionArgs, MetaFunction } from "react-router";
|
|
import { Form, Link, useActionData, useLoaderData, useNavigation } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import { FeatherArrowLeft, FeatherCheckCircle, FeatherInfo, FeatherRefreshCw } from "@subframe/core";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { getRegistrationVerificationViewByToken } from "~/utils/db/registration.db.server";
|
|
import AuthService from "~/modules/users/services/AuthService";
|
|
import { getPasswordChecks, type PasswordPolicy } from "~/modules/users/components/RegisterForm";
|
|
import { loadPasswordPolicy } from "~/services/auth/password-policy.server";
|
|
import { useRootData } from "~/utils/data/useRootData";
|
|
import { AuthShell } from "~/components/auth/AuthShell";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { Card } from "~/ui/components/Card";
|
|
import { IconWithBackground } from "~/ui/components/IconWithBackground";
|
|
import { LinkButton } from "~/ui/components/LinkButton";
|
|
import { Loader } from "~/ui/components/Loader";
|
|
import { Logo } from "~/ui/components/Logo";
|
|
import { TextInput } from "~/ui/components/TextInput";
|
|
|
|
// E-Mail-Bestaetigung -> Passwort setzen -> Tarifaktivierung.
|
|
//
|
|
// Davids S02 ist input-los und auto-provisioniert. Die bestehende Backend-Kette verlangt aber, dass der
|
|
// Nutzer hier ein Passwort setzt (das Registration-Modell hat KEINE Passwort-Spalte, das Passwort wird also
|
|
// erst beim Verify gesammelt) und provisioniert SYNCHRON in der Action (kein Job/Polling). Deshalb:
|
|
// - vor dem Absenden: kompakte Abschluss-Card (Passwort -> Tarifaktivierung); restliche Stammdaten
|
|
// kommen aus der Registration als hidden-Felder (kein erneutes Tippen).
|
|
// - waehrend des Absendens: ehrliche Uebergangsansicht; bei Erfolg leitet die Action in die
|
|
// verpflichtende Tarifaktivierung weiter. Produktzugriff folgt erst nach bestaetigter Zahlung.
|
|
// [Backend-Flags] Davids reines Auto-S02 braucht: Passwort-Spalte auf Registration (Erfassung schon in S01),
|
|
// asynchrone Provisionierung + Polling/Live-Checkliste. Davids "Erneut versuchen"/"Support kontaktieren"
|
|
// sind hier weggelassen (synchroner Flow, keine echten Ziele).
|
|
|
|
export const meta: MetaFunction<typeof loader> = ({ data }) => [{ title: data?.title }];
|
|
|
|
type LoaderData = {
|
|
title: string;
|
|
registration: { email: string } | null;
|
|
creationRequestKey: string;
|
|
verificationStatus: "pending" | "expired" | "used" | "invalid";
|
|
passwordPolicy: PasswordPolicy;
|
|
};
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const { t } = await getTranslations(request);
|
|
const verification = await getRegistrationVerificationViewByToken(params.id ?? "");
|
|
const data: LoaderData = {
|
|
title: `${t("account.verify.title")} | ${process.env.APP_NAME}`,
|
|
registration: verification.status === "pending" ? { email: verification.email } : null,
|
|
creationRequestKey: verification.status === "pending" ? verification.creationRequestKey : "",
|
|
verificationStatus: verification.status,
|
|
passwordPolicy: loadPasswordPolicy(),
|
|
};
|
|
return data;
|
|
};
|
|
|
|
type ActionData = {
|
|
error?: string;
|
|
creationRequestKey?: string;
|
|
fieldErrors?: {
|
|
email: string | undefined;
|
|
password: string | undefined;
|
|
};
|
|
};
|
|
|
|
export const action: ActionFunction = async ({ request, params }) => {
|
|
return await AuthService.verifyFromRequest({ request, params });
|
|
};
|
|
|
|
export default function RegisterRoute() {
|
|
const data = useLoaderData<LoaderData>();
|
|
const actionData = useActionData<ActionData>();
|
|
const { t } = useTranslation();
|
|
const { csrf } = useRootData();
|
|
const navigation = useNavigation();
|
|
const isSubmitting = navigation.state !== "idle";
|
|
const registration = data.registration;
|
|
const canComplete = Boolean(registration);
|
|
const creationRequestKey = actionData?.creationRequestKey ?? data.creationRequestKey;
|
|
const [password, setPassword] = useState("");
|
|
const [passwordValidationRequested, setPasswordValidationRequested] = useState(false);
|
|
const passwordChecks = useMemo(() => getPasswordChecks(password, data.passwordPolicy), [data.passwordPolicy, password]);
|
|
const passwordMeetsClientPolicy = passwordChecks.length > 0 && passwordChecks.every((check) => check.valid);
|
|
const showPasswordPolicyError = passwordValidationRequested && !passwordMeetsClientPolicy;
|
|
|
|
// Ehrliche Uebergangsansicht waehrend Passwort und Registrierung serverseitig abgeschlossen werden.
|
|
if (canComplete && isSubmitting) {
|
|
return (
|
|
<div
|
|
className="bg-surface-neutral-primary mobile:gap-6 mobile:py-6 flex min-h-screen w-full flex-col items-center justify-center gap-8 px-4 py-12"
|
|
data-ui-id="ui.auth.verify.preparing"
|
|
>
|
|
<Logo size="64" />
|
|
<Card size="medium" variant="default" className="max-w-[448px]">
|
|
<div className="flex w-full flex-col items-center gap-6">
|
|
<IconWithBackground size="large" round={true} icon={<FeatherRefreshCw />} />
|
|
<div className="flex w-full flex-col items-center gap-2">
|
|
<span className="text-heading- font-heading- text-text-neutral-primary text-center">{t("account.verify.preparingTitle")}</span>
|
|
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary w-full text-center">
|
|
{t("account.verify.preparingBody")}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<div className="border-border-default bg-surface-neutral-primary flex w-full flex-col items-start gap-3 rounded-md border border-solid px-4 py-4">
|
|
<div className="flex w-full items-center gap-3">
|
|
<FeatherCheckCircle className="text-body-default-regular-150 font-body-default-regular-150 text-text-feedback-positive" />
|
|
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary shrink-0 grow basis-0">
|
|
{t("account.verify.stepAccountCreated")}
|
|
</span>
|
|
</div>
|
|
<div className="flex w-full items-center gap-3">
|
|
<FeatherCheckCircle className="text-body-default-regular-150 font-body-default-regular-150 text-text-feedback-positive" />
|
|
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary shrink-0 grow basis-0">
|
|
{t("account.verify.stepEmailConfirmed")}
|
|
</span>
|
|
</div>
|
|
<div className="flex w-full items-center gap-3">
|
|
<Loader className="text-text-neutral-tertiary" size="small" />
|
|
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-primary shrink-0 grow basis-0">
|
|
{t("account.verify.stepWorkspaceConfiguring")}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<div className="flex w-full items-center justify-center gap-2">
|
|
<FeatherInfo className="text-body-small-regular-150 font-body-small-regular-150 text-icon-neutral-tertiary" />
|
|
<span className="text-body-extra-small-regular-150 font-body-extra-small-regular-150 text-text-neutral-tertiary text-center">
|
|
{t("account.verify.autoRedirectNote")}
|
|
</span>
|
|
</div>
|
|
</Card>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// Abschluss-Card: Passwort setzen -> Tarifaktivierung (Backend sammelt das Passwort erst hier).
|
|
if (canComplete) {
|
|
return (
|
|
<AuthShell
|
|
title={t("account.verify.title")}
|
|
subtitle={t("account.verify.completeSubtitle")}
|
|
cardSize="medium"
|
|
cardClassName="max-w-[448px]"
|
|
logoIconOnly={false}
|
|
dataUiId="ui.auth.verify.page"
|
|
>
|
|
<Form method="post" className="flex w-full flex-col gap-4" data-ui-id="ui.auth.verify.form">
|
|
<input type="hidden" name="csrf" value={csrf} readOnly />
|
|
<input type="hidden" name="creationRequestKey" value={creationRequestKey} readOnly />
|
|
|
|
<div className="border-border-default bg-surface-neutral-primary flex w-full flex-col items-center gap-2 rounded-md border border-solid px-4 py-3">
|
|
<span className="text-body-extra-small-medium-150 font-body-extra-small-medium-150 text-text-neutral-tertiary">
|
|
{t("account.verify.linkSentTo")}
|
|
</span>
|
|
<span className="text-body-default-semibold-0 font-body-default-semibold-0 text-text-neutral-primary w-full text-center">
|
|
{registration?.email}
|
|
</span>
|
|
</div>
|
|
|
|
<div className="flex w-full flex-col items-start gap-2">
|
|
<TextInput
|
|
className="h-auto w-full flex-none"
|
|
fillContainer
|
|
showLabel
|
|
label={t("account.shared.password")}
|
|
helpText={actionData?.fieldErrors?.password ?? ""}
|
|
error={showPasswordPolicyError || Boolean(actionData?.fieldErrors?.password)}
|
|
message={
|
|
showPasswordPolicyError ? (
|
|
<span id="verification-password-policy-error" role="alert" data-ui-id="ui.auth.verify.passwordPolicyError">
|
|
{t("account.register.passwordRequirements.fixBeforeContinue")}
|
|
</span>
|
|
) : undefined
|
|
}
|
|
type="password"
|
|
>
|
|
<TextInput.Input
|
|
name="password"
|
|
type="password"
|
|
required
|
|
autoComplete="new-password"
|
|
autoFocus
|
|
placeholder={t("account.register.passwordChoosePlaceholder")}
|
|
value={password}
|
|
onChange={(event) => {
|
|
setPassword(event.target.value);
|
|
if (passwordValidationRequested) setPasswordValidationRequested(false);
|
|
}}
|
|
onBlur={() => {
|
|
if (password.length > 0 && !passwordMeetsClientPolicy) setPasswordValidationRequested(true);
|
|
}}
|
|
aria-invalid={showPasswordPolicyError || Boolean(actionData?.fieldErrors?.password)}
|
|
aria-describedby={showPasswordPolicyError ? "verification-password-policy-error" : undefined}
|
|
disabled={isSubmitting}
|
|
data-ui-id="ui.auth.verify.password"
|
|
/>
|
|
</TextInput>
|
|
<ul
|
|
className="text-body-extra-small-regular-150 font-body-extra-small-regular-150 flex w-full flex-col gap-1"
|
|
aria-live="polite"
|
|
aria-label={t("account.register.passwordRequirements.label")}
|
|
data-ui-id="ui.auth.verify.passwordRequirements"
|
|
>
|
|
{passwordChecks.map((check) => {
|
|
const label = t(check.label, check.args);
|
|
const status = check.valid ? t("account.register.passwordRequirements.met") : t("account.register.passwordRequirements.notMet");
|
|
return (
|
|
<li
|
|
key={check.key}
|
|
className={
|
|
check.valid ? "text-text-feedback-positive" : showPasswordPolicyError ? "text-text-feedback-negative" : "text-text-neutral-tertiary"
|
|
}
|
|
aria-label={`${status}: ${label}`}
|
|
>
|
|
<span aria-hidden="true">{check.valid ? "[x]" : "[ ]"}</span> {label}
|
|
</li>
|
|
);
|
|
})}
|
|
</ul>
|
|
</div>
|
|
|
|
<Button
|
|
type="submit"
|
|
className="h-9 w-full flex-none"
|
|
loading={isSubmitting}
|
|
disabled={isSubmitting || !passwordMeetsClientPolicy}
|
|
data-ui-id="ui.auth.verify.submit"
|
|
>
|
|
{t("account.verify.setupWorkspace")}
|
|
</Button>
|
|
|
|
{actionData?.error && navigation.state === "idle" ? (
|
|
<p className="text-body-small-regular-0 font-body-small-regular-0 text-text-feedback-negative text-center" role="alert">
|
|
{actionData.error}
|
|
</p>
|
|
) : null}
|
|
</Form>
|
|
</AuthShell>
|
|
);
|
|
}
|
|
|
|
const invalidLinkMessage =
|
|
data.verificationStatus === "expired"
|
|
? t("account.verify.expiredLink")
|
|
: data.verificationStatus === "used"
|
|
? t("account.verify.usedLink")
|
|
: t("account.verify.invalidLink");
|
|
const invalidLinkDestination = data.verificationStatus === "expired" ? "/register" : "/login";
|
|
const invalidLinkAction = data.verificationStatus === "expired" ? t("account.verify.restartRegistration") : t("account.verify.toLogin");
|
|
|
|
// Invalid, expired, or already-consumed link.
|
|
return (
|
|
<AuthShell
|
|
title={t("account.verify.title")}
|
|
subtitle={invalidLinkMessage}
|
|
cardSize="medium"
|
|
cardClassName="max-w-[448px]"
|
|
logoIconOnly={false}
|
|
dataUiId="ui.auth.verify.invalid"
|
|
>
|
|
<div className="flex w-full items-center justify-center gap-1" data-ui-id="ui.auth.verify.toLogin">
|
|
<Link to={invalidLinkDestination} className="no-underline">
|
|
<LinkButton size="small" icon={<FeatherArrowLeft />}>
|
|
{invalidLinkAction}
|
|
</LinkButton>
|
|
</Link>
|
|
</div>
|
|
</AuthShell>
|
|
);
|
|
}
|
|
|
|
export function headers() {
|
|
return {
|
|
"Cache-Control": "no-store",
|
|
"Referrer-Policy": "no-referrer",
|
|
};
|
|
}
|