Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
102 lines
3.1 KiB
TypeScript
102 lines
3.1 KiB
TypeScript
import { useStableCallback } from '@rocket.chat/fuselage-hooks';
|
|
import { Page } from '@rocket.chat/ui-client';
|
|
import {
|
|
useEndpoint,
|
|
useLoginWithToken,
|
|
useRouteParameter,
|
|
useRouter,
|
|
useSetModal,
|
|
useToastMessageDispatch,
|
|
} from '@rocket.chat/ui-contexts';
|
|
import { useEffect } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import TwoFactorModal from '../../components/TwoFactorModal/TwoFactorModal';
|
|
|
|
const throwErrorOnInvalidMethod = (method: never): never => {
|
|
throw new Error(`Invalid Two Factor method: ${method}`);
|
|
};
|
|
|
|
const OAuthTwoFactorAuthenticationRouter = () => {
|
|
const method = useRouteParameter('method') as 'totp' | 'email' | undefined;
|
|
const challengeId = useRouteParameter('challengeId');
|
|
const router = useRouter();
|
|
const dispatchToastMessage = useToastMessageDispatch();
|
|
const setModal = useSetModal();
|
|
const loginWithToken = useLoginWithToken();
|
|
const { t } = useTranslation();
|
|
const verifyChallenge = useEndpoint('POST', '/v1/twoFactorChallenges.verifyChallenge');
|
|
const sendEmailCode = useEndpoint('POST', '/v1/twoFactorChallenges.sendEmailCode');
|
|
|
|
const navigateToHome = useStableCallback(() => {
|
|
setModal(null);
|
|
router.navigate('/home', { replace: true });
|
|
});
|
|
|
|
const resendEmail = useStableCallback(async () => {
|
|
if (!challengeId) {
|
|
return null;
|
|
}
|
|
await sendEmailCode({ challengeId });
|
|
return null;
|
|
});
|
|
|
|
const onConfirm = useStableCallback(async (code: string) => {
|
|
if (!challengeId || !code) {
|
|
return;
|
|
}
|
|
try {
|
|
const { loginToken, userId } = await verifyChallenge({ challengeId, code });
|
|
|
|
const { loginClient } = router.getSearchParameters();
|
|
|
|
if (loginClient === 'mobile' || loginClient === 'desktop') {
|
|
setModal(null);
|
|
router.navigate({ name: 'home', search: { resumeToken: loginToken, userId, loginClient } }, { replace: true });
|
|
return;
|
|
}
|
|
|
|
await loginWithToken(loginToken);
|
|
navigateToHome();
|
|
} catch (error: any) {
|
|
console.error('Failed to verify challenge', error);
|
|
if (error.errorType === 'totp-max-attempts') {
|
|
setModal(null);
|
|
dispatchToastMessage({ type: 'error', message: t('Maximum_number_of_attempts_reached_please_try_again_later') });
|
|
router.navigate('/login', { replace: true });
|
|
return;
|
|
}
|
|
if (error.errorType === 'error-challenge-expired' || error.errorType === 'error-challenge-not-found') {
|
|
setModal(null);
|
|
dispatchToastMessage({ type: 'error', message: t('Challenge_expired_please_try_again_later') });
|
|
router.navigate('/login', { replace: true });
|
|
return;
|
|
}
|
|
throw error;
|
|
}
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (!method || !challengeId) {
|
|
router.navigate('/home');
|
|
return;
|
|
}
|
|
|
|
if (method === 'email') {
|
|
setModal(<TwoFactorModal method={method} onConfirm={onConfirm} challengeId={challengeId} onClose={navigateToHome} />);
|
|
return;
|
|
}
|
|
|
|
if (method === 'totp') {
|
|
setModal(<TwoFactorModal method={method} onConfirm={onConfirm} onClose={navigateToHome} />);
|
|
return;
|
|
}
|
|
|
|
throwErrorOnInvalidMethod(method);
|
|
}, [method, challengeId, router, setModal, onConfirm, resendEmail, navigateToHome]);
|
|
|
|
return <Page />;
|
|
};
|
|
|
|
export default OAuthTwoFactorAuthenticationRouter;
|