diff --git a/apps/dokploy/components/dashboard/settings/profile/enable-2fa.tsx b/apps/dokploy/components/dashboard/settings/profile/enable-2fa.tsx index 1cfa7574eb..afc859f41a 100644 --- a/apps/dokploy/components/dashboard/settings/profile/enable-2fa.tsx +++ b/apps/dokploy/components/dashboard/settings/profile/enable-2fa.tsx @@ -61,61 +61,17 @@ export const Enable2FA = () => { const [isDialogOpen, setIsDialogOpen] = useState(false); const [step, setStep] = useState<"password" | "verify">("password"); const [isPasswordLoading, setIsPasswordLoading] = useState(false); + const [otpValue, setOtpValue] = useState(""); - const handlePasswordSubmit = async (formData: PasswordForm) => { - setIsPasswordLoading(true); - try { - const { data: enableData, error } = await authClient.twoFactor.enable({ - password: formData.password, - issuer: formData.issuer, - }); - - if (!enableData) { - throw new Error(error?.message || "Error enabling 2FA"); - } - - if (enableData.backupCodes) { - setBackupCodes(enableData.backupCodes); - } - - if (enableData.totpURI) { - const qrCodeUrl = await QRCode.toDataURL(enableData.totpURI); - - setData({ - qrCodeUrl, - secret: enableData.totpURI.split("secret=")[1]?.split("&")[0] || "", - totpURI: enableData.totpURI, - }); - - setStep("verify"); - toast.success("Scan the QR code with your authenticator app"); - } else { - throw new Error("No TOTP URI received from server"); - } - } catch (error) { - toast.error( - error instanceof Error ? error.message : "Error setting up 2FA", - ); - passwordForm.setError("password", { - message: - error instanceof Error ? error.message : "Error setting up 2FA", - }); - } finally { - setIsPasswordLoading(false); - } - }; - - const handleVerifySubmit = async (formData: PinForm) => { + const handleVerifySubmit = async (e: React.FormEvent) => { + e.preventDefault(); try { const result = await authClient.twoFactor.verifyTotp({ - code: formData.pin, + code: otpValue, }); if (result.error) { if (result.error.code === "INVALID_TWO_FACTOR_AUTHENTICATION") { - pinForm.setError("pin", { - message: "Invalid code. Please try again.", - }); toast.error("Invalid verification code"); return; } @@ -137,15 +93,11 @@ export const Enable2FA = () => { ? "Connection error. Please check your internet connection." : error.message; - pinForm.setError("pin", { - message: errorMessage, - }); toast.error(errorMessage); } else { - pinForm.setError("pin", { - message: "Error verifying code", + toast.error("Error verifying 2FA code", { + description: error instanceof Error ? error.message : "Unknown error", }); - toast.error("Error verifying 2FA code"); } } }; @@ -169,10 +121,62 @@ export const Enable2FA = () => { setStep("password"); setData(null); setBackupCodes([]); - passwordForm.reset(); - pinForm.reset(); + setOtpValue(""); + passwordForm.reset({ + password: "", + issuer: "", + }); } - }, [isDialogOpen, passwordForm, pinForm]); + }, [isDialogOpen, passwordForm]); + + useEffect(() => { + if (step === "verify") { + setOtpValue(""); + } + }, [step]); + + const handlePasswordSubmit = async (formData: PasswordForm) => { + setIsPasswordLoading(true); + try { + const { data: enableData, error } = await authClient.twoFactor.enable({ + password: formData.password, + issuer: formData.issuer, + }); + + if (!enableData) { + throw new Error(error?.message || "Error enabling 2FA"); + } + + if (enableData.backupCodes) { + setBackupCodes(enableData.backupCodes); + } + + if (enableData.totpURI) { + const qrCodeUrl = await QRCode.toDataURL(enableData.totpURI); + + setData({ + qrCodeUrl, + secret: enableData.totpURI.split("secret=")[1]?.split("&")[0] || "", + totpURI: enableData.totpURI, + }); + + setStep("verify"); + toast.success("Scan the QR code with your authenticator app"); + } else { + throw new Error("No TOTP URI received from server"); + } + } catch (error) { + toast.error( + error instanceof Error ? error.message : "Error setting up 2FA", + ); + passwordForm.setError("password", { + message: + error instanceof Error ? error.message : "Error setting up 2FA", + }); + } finally { + setIsPasswordLoading(false); + } + }; return ( @@ -233,7 +237,8 @@ export const Enable2FA = () => { /> - Enter your password to enable 2FA + Use a custom issuer to identify the service you're + authenticating with. @@ -250,11 +255,7 @@ export const Enable2FA = () => { ) : (
- +
{data?.qrCodeUrl ? ( <> @@ -306,36 +307,33 @@ export const Enable2FA = () => { )}
- ( - - Verification Code - - - - - - - - - - - - - - Enter the 6-digit code from your authenticator app - - - - )} - /> +
+ Verification Code + + + + + + + + + + + + Enter the 6-digit code from your authenticator app + +