mirror of
https://github.com/docmost/docmost.git
synced 2025-11-15 06:31:10 +10:00
* feat(EE): MFA implementation for enterprise edition - Add TOTP-based two-factor authentication - Add backup codes support - Add MFA enforcement at workspace level - Add MFA setup and challenge UI pages - Support MFA for login and password reset flows - Add MFA validation for secure pages * fix types * remove unused object * sync * remove unused type * sync * refactor: rename MFA enabled field to is_enabled * sync
52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { useNavigate, useLocation } from "react-router-dom";
|
|
import APP_ROUTE from "@/lib/app-route";
|
|
import { validateMfaAccess } from "@/ee/mfa";
|
|
|
|
export function useMfaPageProtection() {
|
|
const navigate = useNavigate();
|
|
const location = useLocation();
|
|
const [isValidating, setIsValidating] = useState(true);
|
|
const [isValid, setIsValid] = useState(false);
|
|
|
|
useEffect(() => {
|
|
const checkAccess = async () => {
|
|
const result = await validateMfaAccess();
|
|
|
|
if (!result.valid) {
|
|
navigate(APP_ROUTE.AUTH.LOGIN);
|
|
return;
|
|
}
|
|
|
|
// Check if user is on the correct page based on their MFA state
|
|
const isOnChallengePage =
|
|
location.pathname === APP_ROUTE.AUTH.MFA_CHALLENGE;
|
|
const isOnSetupPage =
|
|
location.pathname === APP_ROUTE.AUTH.MFA_SETUP_REQUIRED;
|
|
|
|
if (result.requiresMfaSetup && !isOnSetupPage) {
|
|
// User needs to set up MFA but is on challenge page
|
|
navigate(APP_ROUTE.AUTH.MFA_SETUP_REQUIRED);
|
|
} else if (
|
|
!result.requiresMfaSetup &&
|
|
result.userHasMfa &&
|
|
!isOnChallengePage
|
|
) {
|
|
// User has MFA and should be on challenge page
|
|
navigate(APP_ROUTE.AUTH.MFA_CHALLENGE);
|
|
} else if (!result.isTransferToken) {
|
|
// User has a regular auth token, shouldn't be on MFA pages
|
|
navigate(APP_ROUTE.HOME);
|
|
} else {
|
|
setIsValid(true);
|
|
}
|
|
|
|
setIsValidating(false);
|
|
};
|
|
|
|
checkAccess();
|
|
}, [navigate, location.pathname]);
|
|
|
|
return { isValidating, isValid };
|
|
}
|