100 lines
3.4 KiB
TypeScript
100 lines
3.4 KiB
TypeScript
import { useNavigate, type LoaderFunctionArgs } from "react-router";
|
|
import { useLoaderData } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import { FeatherAlertTriangle, FeatherArrowLeft, FeatherWand2 } from "@subframe/core";
|
|
import { consumeMagicLinkToken } from "~/services/auth/magic-link.server";
|
|
import { getUser } from "~/utils/db/users.db.server";
|
|
import { createUserSession, getUserInfo, setLoggedUser } from "~/utils/session.server";
|
|
import { AuthStatus } from "~/components/auth/AuthShell";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { LinkButton } from "~/ui/components/LinkButton";
|
|
|
|
type LoaderData = {
|
|
status: "invalid" | "expired" | "replayed" | "not-found" | "purpose-mismatch";
|
|
};
|
|
|
|
export async function loader({ request }: LoaderFunctionArgs): Promise<Response | LoaderData> {
|
|
const url = new URL(request.url);
|
|
const token = url.searchParams.get("token");
|
|
if (!token) {
|
|
return { status: "invalid" };
|
|
}
|
|
const result = await consumeMagicLinkToken(token, { expectedPurpose: "login" });
|
|
if (!result.ok) {
|
|
return { status: result.reason };
|
|
}
|
|
const user = await getUser(result.userId);
|
|
if (!user) {
|
|
return { status: "not-found" };
|
|
}
|
|
const userInfo = await getUserInfo(request);
|
|
const userSession = await setLoggedUser({
|
|
id: user.id,
|
|
email: user.email,
|
|
defaultTenantId: user.defaultTenantId,
|
|
});
|
|
return createUserSession(
|
|
{
|
|
...userInfo,
|
|
...userSession,
|
|
lng: user.locale ?? userInfo.lng,
|
|
},
|
|
"/"
|
|
);
|
|
}
|
|
|
|
const REASON_MESSAGE_KEYS: Record<LoaderData["status"], { title: string; description: string }> = {
|
|
invalid: {
|
|
title: "auth:magicLink.invalid.title",
|
|
description: "auth:magicLink.invalid.description",
|
|
},
|
|
expired: {
|
|
title: "auth:magicLink.expired.title",
|
|
description: "auth:magicLink.expired.description",
|
|
},
|
|
replayed: {
|
|
title: "auth:magicLink.replayed.title",
|
|
description: "auth:magicLink.replayed.description",
|
|
},
|
|
"not-found": {
|
|
title: "auth:magicLink.invalid.title",
|
|
description: "auth:magicLink.invalid.description",
|
|
},
|
|
"purpose-mismatch": {
|
|
title: "auth:magicLink.purposeMismatch.title",
|
|
description: "auth:magicLink.purposeMismatch.description",
|
|
},
|
|
};
|
|
|
|
export default function MagicLinkAuthRoute() {
|
|
const data = useLoaderData<LoaderData>();
|
|
const { t } = useTranslation();
|
|
const navigate = useNavigate();
|
|
const message = REASON_MESSAGE_KEYS[data.status] ?? REASON_MESSAGE_KEYS.invalid;
|
|
// D-D0a — Davids "Magic Link _ Invalid Link"-Design (AuthStatus), Reason-spezifische Texte bleiben.
|
|
// "Neuen Link anfordern" + "Zurueck zur Anmeldung" fuehren auf /login (dort liegt der Magic-Link-Request).
|
|
return (
|
|
<AuthStatus
|
|
icon={<FeatherAlertTriangle />}
|
|
tone="negative"
|
|
title={t(message.title)}
|
|
message={t(message.description)}
|
|
dataUiId="ui.auth.magicLink.invalid"
|
|
>
|
|
<Button icon={<FeatherWand2 />} onClick={() => navigate("/login")} data-ui-id="ui.auth.magicLink.requestNew">
|
|
{t("account.login.magicLinkRequestNew")}
|
|
</Button>
|
|
<LinkButton size="small" icon={<FeatherArrowLeft />} onClick={() => navigate("/login")} data-ui-id="ui.auth.magicLink.backToLogin">
|
|
{t("account.login.backToLogin")}
|
|
</LinkButton>
|
|
</AuthStatus>
|
|
);
|
|
}
|
|
|
|
export function headers() {
|
|
return {
|
|
"Cache-Control": "no-store",
|
|
"Referrer-Policy": "no-referrer",
|
|
};
|
|
}
|