Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

create reusable component

Thiago Araujo da Silva94dfed6b8932
diff --git a/frontend/src/components/mfa/MfaEnrollment.tsx b/frontend/src/components/mfa/MfaEnrollment.tsx
index 591c0913d0..f607849d23 100644
--- a/frontend/src/components/mfa/MfaEnrollment.tsx
+++ b/frontend/src/components/mfa/MfaEnrollment.tsx
@@ -1,15 +1,14 @@
import { useEffect, useRef, useState } from "react";
-import { TriangleAlertIcon } from "lucide-react";
import { createNotification } from "@app/components/notifications";
import SecurityClient from "@app/components/utilities/SecurityClient";
import { ContentLoader } from "@app/components/v2";
-import { Alert, AlertDescription, AlertTitle, Button } from "@app/components/v3";
+import { Button } from "@app/components/v3";
import { MfaMethod } from "@app/hooks/api/auth/types";
import { getMfaTempToken } from "@app/hooks/api/reactQuery";
import { useUpdateUserMfa } from "@app/hooks/api/users";
-import { MFA_METHOD_LABELS, RecoveryCodesView, VerifyStep } from "./setup";
+import { MFA_METHOD_LABELS, RecoveryCodesStep, VerifyStep } from "./setup";
type Props = {
method: MfaMethod;
@@ -90,33 +89,11 @@ export const MfaEnrollment = ({ method, onComplete }: Props) => {
{phase === "recovery" && (
<div className="flex flex-col gap-4">
- {hasRecoveryCodes ? (
- <>
- <Alert variant="warning">
- <TriangleAlertIcon />
- <AlertTitle>Save your recovery codes</AlertTitle>
- <AlertDescription>
- Store these somewhere safe. Each works once if you lose access to your other
- methods.
- </AlertDescription>
- </Alert>
- <RecoveryCodesView recoveryCodes={recoveryCodes} onSaved={() => setHasSaved(true)} />
- </>
- ) : (
- <Alert variant="danger">
- <TriangleAlertIcon />
- <AlertTitle>Couldn&apos;t load recovery codes</AlertTitle>
- <AlertDescription>
- Two-factor authentication is enabled, but we couldn&apos;t display your recovery
- codes. Regenerate them from your security settings after signing in so you
- don&apos;t get locked out.
- </AlertDescription>
- </Alert>
- )}
+ <RecoveryCodesStep recoveryCodes={recoveryCodes} onSaved={() => setHasSaved(true)} />
<Button
variant="org"
isFullWidth
- isDisabled={!hasRecoveryCodes || !hasSaved}
+ isDisabled={hasRecoveryCodes && !hasSaved}
onClick={handleContinue}
>
Continue
diff --git a/frontend/src/components/mfa/setup/RecoveryCodesStep.tsx b/frontend/src/components/mfa/setup/RecoveryCodesStep.tsx
new file mode 100644
index 0000000000..f4cea6604e
--- /dev/null
+++ b/frontend/src/components/mfa/setup/RecoveryCodesStep.tsx
@@ -0,0 +1,43 @@
+import { TriangleAlertIcon } from "lucide-react";
+
+import { Alert, AlertDescription, AlertTitle } from "@app/components/v3";
+
+import { RecoveryCodesView } from "./RecoveryCodesView";
+
+type Props = {
+ recoveryCodes: string[];
+ onSaved: () => void;
+};
+
+// Shared final step for the MFA enrollment and settings wizard: shows the newly
+// minted recovery codes with a save prompt, or a danger fallback when enabling
+// MFA returned no codes (e.g. MFA was already on) so the user knows to
+// regenerate them from their security settings.
+export const RecoveryCodesStep = ({ recoveryCodes, onSaved }: Props) => {
+ if (recoveryCodes.length === 0) {
+ return (
+ <Alert variant="danger">
+ <TriangleAlertIcon />
+ <AlertTitle>Couldn&apos;t load recovery codes</AlertTitle>
+ <AlertDescription>
+ Two-factor authentication is enabled, but we couldn&apos;t display your recovery codes.
+ Regenerate them from your security settings so you don&apos;t get locked out.
+ </AlertDescription>
+ </Alert>
+ );
+ }
+
+ return (
+ <>
+ <Alert variant="warning">
+ <TriangleAlertIcon />
+ <AlertTitle>Save your recovery codes</AlertTitle>
+ <AlertDescription>
+ Store these somewhere safe. Each code works once and lets you sign in if you lose access
+ to your other methods.
+ </AlertDescription>
+ </Alert>
+ <RecoveryCodesView recoveryCodes={recoveryCodes} onSaved={onSaved} />
+ </>
+ );
+};
diff --git a/frontend/src/components/mfa/setup/index.ts b/frontend/src/components/mfa/setup/index.ts
index 3596372ec4..1baf22dcd6 100644
--- a/frontend/src/components/mfa/setup/index.ts
+++ b/frontend/src/components/mfa/setup/index.ts
@@ -1,4 +1,5 @@
export { MethodStep } from "./MethodStep";
+export { RecoveryCodesStep } from "./RecoveryCodesStep";
export { RecoveryCodesView } from "./RecoveryCodesView";
export type { MfaMethodOption, WizardStep } from "./types";
export { MFA_METHOD_ICONS, MFA_METHOD_LABELS, MFA_METHOD_OPTIONS, WIZARD_STEPS } from "./types";
diff --git a/frontend/src/pages/user/PersonalSettingsPage/components/SecuritySection/MfaSetupWizard/MfaSetupWizard.tsx b/frontend/src/pages/user/PersonalSettingsPage/components/SecuritySection/MfaSetupWizard/MfaSetupWizard.tsx
index 7a1f55c575..7c9fd0564c 100644
--- a/frontend/src/pages/user/PersonalSettingsPage/components/SecuritySection/MfaSetupWizard/MfaSetupWizard.tsx
+++ b/frontend/src/pages/user/PersonalSettingsPage/components/SecuritySection/MfaSetupWizard/MfaSetupWizard.tsx
@@ -1,13 +1,9 @@
import { useEffect, useState } from "react";
import { useQueryClient } from "@tanstack/react-query";
-import { TriangleAlertIcon } from "lucide-react";
-import { MethodStep, RecoveryCodesView, VerifyStep, WIZARD_STEPS } from "@app/components/mfa/setup";
+import { MethodStep, RecoveryCodesStep, VerifyStep, WIZARD_STEPS } from "@app/components/mfa/setup";
import { createNotification } from "@app/components/notifications";
import {
- Alert,
- AlertDescription,
- AlertTitle,
Button,
Sheet,
SheetContent,
@@ -105,32 +101,7 @@ export const MfaSetupWizard = ({ isOpen, onOpenChange }: Props) => {
{step === 1 && <VerifyStep method={selectedMethod} onVerified={handleVerified} />}
{step === 2 && (
<div className="flex flex-col gap-4">
- {hasRecoveryCodes ? (
- <>
- <Alert variant="warning">
- <TriangleAlertIcon />
- <AlertTitle>Save your recovery codes</AlertTitle>
- <AlertDescription>
- Store these somewhere safe. Each code works once and lets you sign in if you
- lose access to your other methods.
- </AlertDescription>
- </Alert>
- <RecoveryCodesView
- recoveryCodes={recoveryCodes}
- onSaved={() => setHasSaved(true)}
- />
- </>
- ) : (
- <Alert variant="danger">
- <TriangleAlertIcon />
- <AlertTitle>Couldn&apos;t load recovery codes</AlertTitle>
- <AlertDescription>
- Two-factor authentication is enabled, but we couldn&apos;t display your recovery
- codes. Regenerate them from the Recovery options section so you don&apos;t get
- locked out.
- </AlertDescription>
- </Alert>
- )}
+ <RecoveryCodesStep recoveryCodes={recoveryCodes} onSaved={() => setHasSaved(true)} />
</div>
)}
</div>
@@ -151,11 +122,7 @@ export const MfaSetupWizard = ({ isOpen, onOpenChange }: Props) => {
</Button>
)}
{step === 2 && (
- <Button
- variant="org"
- isDisabled={!hasRecoveryCodes || !hasSaved}
- onClick={handleDone}
- >
+ <Button variant="org" isDisabled={hasRecoveryCodes && !hasSaved} onClick={handleDone}>
Done
</Button>
)}
diff --git a/frontend/src/pages/user/PersonalSettingsPage/components/SecuritySection/useRecoveryCodesMfa.ts b/frontend/src/pages/user/PersonalSettingsPage/components/SecuritySection/useRecoveryCodesMfa.ts
index 8a2b222015..6d2c0ae969 100644
--- a/frontend/src/pages/user/PersonalSettingsPage/components/SecuritySection/useRecoveryCodesMfa.ts
+++ b/frontend/src/pages/user/PersonalSettingsPage/components/SecuritySection/useRecoveryCodesMfa.ts
@@ -55,6 +55,7 @@ export const useRecoveryCodesMfa = () => {
try {
if (Date.now() - startTime > MFA_TIMEOUT) {
clearInterval(pollIntervalRef.current);
+ if (popupRef.current && !popupRef.current.closed) popupRef.current.close();
createNotification({
type: "error",
text: "MFA verification timed out. If a popup did not appear, check that popups are not blocked for this site."