import { getUserTotpStatus } from "~/utils/db/users.db.server"; import { commitPendingMfaLoginCookie, type PendingMfaPrimaryMethod } from "~/utils/session.server"; export const LOGIN_MFA_CHALLENGE_PATH = "/login-mfa"; export type AlternativeLoginMfaResult = | { required: false } | { required: true; setCookie: string }; /** * Alternative primary factors without a separately verified upstream MFA * assurance must not silently satisfy a configured second factor. Trusted * device bypass is intentionally excluded from these flows: an enabled TOTP * configuration always transitions to the pending MFA state. */ export async function beginAlternativeLoginMfa({ request, userId, redirectTo, primaryMethod, }: { request: Request; userId: string; redirectTo: string; primaryMethod: PendingMfaPrimaryMethod; }): Promise { const status = await getUserTotpStatus(userId); if (!status) { throw new Error("MFA status is unavailable for the authenticated identity"); } if (!status.enabled) { return { required: false }; } return { required: true, setCookie: await commitPendingMfaLoginCookie(request, { userId, redirectTo, primaryMethod }), }; }