139 lines
4.9 KiB
TypeScript
139 lines
4.9 KiB
TypeScript
import { redirect, 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";
|
|
import { beginAlternativeLoginMfa, LOGIN_MFA_CHALLENGE_PATH } from "~/services/auth/alternative-login-mfa.server";
|
|
import { loadRequiredWebConfig } from "@hivegrid/config";
|
|
import {
|
|
AdminFallbackLoginRejected,
|
|
assertAdminFallbackLoginAllowedForIdentity,
|
|
isCompletedAdminFallbackLoginResponse,
|
|
} from "~/services/auth/admin-fallback.server";
|
|
import { writeAdminSsoAuditEvent } from "~/modules/admin-sso/audit.server";
|
|
|
|
type LoaderData = {
|
|
status: "invalid" | "expired" | "replayed" | "not-found" | "purpose-mismatch";
|
|
};
|
|
|
|
export async function loader({ request }: LoaderFunctionArgs): Promise<Response | LoaderData> {
|
|
if (!loadRequiredWebConfig().magicLink.enabled) {
|
|
return { status: "invalid" };
|
|
}
|
|
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" };
|
|
}
|
|
try {
|
|
assertAdminFallbackLoginAllowedForIdentity(user);
|
|
} catch (error) {
|
|
if (!(error instanceof AdminFallbackLoginRejected)) {
|
|
throw error;
|
|
}
|
|
await writeAdminSsoAuditEvent({
|
|
action: "admin.login.fallback.failure",
|
|
actorAdminUserId: user.admin?.userId,
|
|
request,
|
|
meta: { reason: error.reason, email: user.email, method: "magic-link", stage: "consume" },
|
|
});
|
|
return { status: "invalid" };
|
|
}
|
|
const mfa = await beginAlternativeLoginMfa({ request, userId: user.id, redirectTo: "/", primaryMethod: "magic-link" });
|
|
if (mfa.required) {
|
|
return redirect(LOGIN_MFA_CHALLENGE_PATH, { headers: { "Set-Cookie": mfa.setCookie } });
|
|
}
|
|
const userInfo = await getUserInfo(request);
|
|
const userSession = await setLoggedUser({
|
|
id: user.id,
|
|
email: user.email,
|
|
defaultTenantId: user.defaultTenantId,
|
|
});
|
|
const response = await createUserSession(
|
|
{
|
|
...userInfo,
|
|
...userSession,
|
|
lng: user.locale ?? userInfo.lng,
|
|
},
|
|
"/",
|
|
{ request, rotate: true },
|
|
);
|
|
if (user.admin && isCompletedAdminFallbackLoginResponse(response)) {
|
|
await writeAdminSsoAuditEvent({
|
|
action: "admin.login.fallback.success",
|
|
actorAdminUserId: user.admin.userId,
|
|
request,
|
|
meta: { email: user.email, severity: "warn", method: "magic-link" },
|
|
});
|
|
}
|
|
return response;
|
|
}
|
|
|
|
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",
|
|
};
|
|
}
|