fix(2fa): improve error handling and display for two-factor authentication setup

- Handle potential errors when enabling two-factor authentication
- Provide more detailed error messages for password verification
- Update client-side error handling to display specific error messages
- Add base URL configuration for non-cloud environments
This commit is contained in:
Mauricio Siu
2025-03-08 12:37:50 -06:00
parent b64ddf1119
commit a6de744f91
2 changed files with 6 additions and 3 deletions

View File

@@ -64,12 +64,12 @@ export const Enable2FA = () => {
const handlePasswordSubmit = async (formData: PasswordForm) => {
setIsPasswordLoading(true);
try {
const { data: enableData } = await authClient.twoFactor.enable({
const { data: enableData, error } = await authClient.twoFactor.enable({
password: formData.password,
});
if (!enableData) {
throw new Error("No data received from server");
throw new Error(error?.message || "Unknown error");
}
if (enableData.backupCodes) {
@@ -95,7 +95,7 @@ export const Enable2FA = () => {
error instanceof Error ? error.message : "Error setting up 2FA",
);
passwordForm.setError("password", {
message: "Error verifying password",
message: `Error verifying password: ${error instanceof Error ? error.message : "Unknown error"}`,
});
} finally {
setIsPasswordLoading(false);

View File

@@ -14,6 +14,9 @@ const { handler, api } = betterAuth({
provider: "pg",
schema: schema,
}),
...(!IS_CLOUD && {
baseURL: "http://localhost:3000",
}),
logger: {
disabled: process.env.NODE_ENV === "production",
},