36 lines
1.4 KiB
TypeScript
36 lines
1.4 KiB
TypeScript
import { redirect, type ActionFunctionArgs, type LoaderFunctionArgs } from "react-router";
|
|
import { serverOnly$ } from "vite-env-only/macros";
|
|
import AuthService from "~/modules/users/services/AuthService";
|
|
import { getUserInfo } from "~/utils/session.server";
|
|
|
|
// D1 — tenant-faehige Verify-Route fuer das 2FA-Login-Gate. Der Login (AuthService.loginFromRequest)
|
|
// schreibt nach Passwort-OK den Pending-Zustand (`pendingMfaUserId`) ins Cookie und schickt den
|
|
// Client hierher. Diese Route liest den Pending-Zustand, prueft den zweiten Faktor und erstellt
|
|
// erst dann die echte Session. Route wird per Ordner-Konvention autoregistriert (login-mfa.tsx).
|
|
|
|
export type LoaderData = {
|
|
redirectTo: string;
|
|
};
|
|
|
|
export type ActionData = {
|
|
error?: string;
|
|
};
|
|
|
|
export const loader = serverOnly$(async ({ request }: LoaderFunctionArgs) => {
|
|
const userInfo = await getUserInfo(request);
|
|
// Bereits voll eingeloggt -> hier nichts zu tun.
|
|
if (userInfo.userId) {
|
|
throw redirect("/");
|
|
}
|
|
// Kein laufender 2FA-Login -> zurueck zur Anmeldung.
|
|
if (!userInfo.pendingMfaUserId) {
|
|
throw redirect("/login");
|
|
}
|
|
return Response.json({ redirectTo: userInfo.pendingMfaRedirectTo ?? "" } satisfies LoaderData);
|
|
})!;
|
|
|
|
export const action = serverOnly$(async ({ request }: ActionFunctionArgs) => {
|
|
const form = await request.formData();
|
|
return AuthService.verifyMfaLoginFromRequest(request, form);
|
|
})!;
|