85 lines
4.0 KiB
TypeScript
85 lines
4.0 KiB
TypeScript
import { redirect, useLoaderData, type ActionFunctionArgs, type LoaderFunctionArgs, type MetaFunction } from "react-router";
|
|
|
|
import { buildSessionListVM, type SessionListVM } from "@toolctrl/view-models";
|
|
import SecuritySessionsScreen from "~/modules/users/components/SecuritySessionsScreen";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { requireAuth } from "~/utils/loaders.middleware";
|
|
import { getUserInfo, validateCSRFToken } from "~/utils/session.server";
|
|
import {
|
|
getActiveSessionByToken,
|
|
listActiveUserSessions,
|
|
markSessionTrusted,
|
|
revokeAllUserSessionsExcept,
|
|
revokeUserSession,
|
|
} from "~/services/auth/sessionStore.server";
|
|
import { listTrustedDevices, revokeAllTrustedDevices, revokeTrustedDevice } from "~/utils/auth/mfa/trusted-device.server";
|
|
|
|
// R-P2a S4b + D1 — Settings > Sicherheit: Geräteliste + Revoke + "überall abmelden" + Trust-Device.
|
|
// Der Loader baut das VM serverseitig (M013), die Inline-UI unten ist das minimale Swap-Ziel für
|
|
// Davids D-P2-Subframe-Screen (über den ui-app-Adapter, M015) — VM und Action-Callbacks bleiben.
|
|
// D1: zusätzlich die per Login-2FA als vertrauenswürdig markierten Geräte (TrustedDevice) — widerrufbar
|
|
// einzeln / "alle vergessen" (DoD-Screen "Trust-Device-Revoke").
|
|
|
|
type TrustedDeviceVM = { id: string; label: string | null; approxLocation: string | null };
|
|
type LoaderData = { title: string; csrf: string; vm: SessionListVM; trustedDevices: TrustedDeviceVM[] };
|
|
|
|
export const meta: MetaFunction<typeof loader> = ({ data }) => [{ title: data?.title }];
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs): Promise<LoaderData> => {
|
|
await requireAuth({ request, params });
|
|
const { t } = await getTranslations(request);
|
|
const userInfo = await getUserInfo(request);
|
|
const sessions = await listActiveUserSessions(userInfo.userId);
|
|
const current = userInfo.sessionToken ? await getActiveSessionByToken(userInfo.sessionToken) : null;
|
|
const vm = buildSessionListVM({
|
|
sessions: sessions.map((session) => ({
|
|
id: session.id,
|
|
lastSeenAt: session.lastSeenAt,
|
|
userAgent: session.userAgent,
|
|
deviceLabel: session.deviceLabel,
|
|
approxLocation: session.approxLocation,
|
|
trustedAt: session.trustedAt,
|
|
})),
|
|
currentSessionId: current?.id ?? null,
|
|
nowMs: Date.now(),
|
|
});
|
|
const trustedDevices: TrustedDeviceVM[] = (await listTrustedDevices(userInfo.userId)).map((device) => ({
|
|
id: device.id,
|
|
label: device.deviceLabel ?? device.userAgent ?? null,
|
|
approxLocation: device.approxLocation,
|
|
}));
|
|
return { title: `${t("settings.security.title")} | ${process.env.APP_NAME ?? "HiveGrid"}`, csrf: userInfo.csrf ?? "", vm, trustedDevices };
|
|
};
|
|
|
|
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
|
await requireAuth({ request, params });
|
|
const form = await request.formData();
|
|
await validateCSRFToken(request, { formData: form });
|
|
const userInfo = await getUserInfo(request);
|
|
const userId = userInfo.userId;
|
|
if (!userId) {
|
|
throw redirect("/login");
|
|
}
|
|
const current = userInfo.sessionToken ? await getActiveSessionByToken(userInfo.sessionToken) : null;
|
|
const intent = form.get("intent");
|
|
const sessionId = String(form.get("sessionId") ?? "");
|
|
const deviceId = String(form.get("deviceId") ?? "");
|
|
if (intent === "revoke" && sessionId) {
|
|
await revokeUserSession({ userId, sessionId });
|
|
} else if (intent === "revoke-all") {
|
|
await revokeAllUserSessionsExcept({ userId, sessionId: current?.id ?? "" });
|
|
} else if (intent === "trust" && sessionId) {
|
|
await markSessionTrusted({ userId, sessionId });
|
|
} else if (intent === "forget-device" && deviceId) {
|
|
await revokeTrustedDevice({ userId, deviceId });
|
|
} else if (intent === "forget-all-devices") {
|
|
await revokeAllTrustedDevices(userId);
|
|
}
|
|
return redirect(new URL(request.url).pathname);
|
|
};
|
|
|
|
export default function SecurityRoute() {
|
|
const { csrf, vm, trustedDevices } = useLoaderData<typeof loader>();
|
|
return <SecuritySessionsScreen vm={vm} trustedDevices={trustedDevices} csrf={csrf} />;
|
|
}
|