Refactor EmailChangeDialog error handling

This commit is contained in:
gpt-engineer-app[bot]
2025-10-14 19:27:14 +00:00
parent 0a325d7c37
commit 1554254c82

View File

@@ -20,7 +20,8 @@ import {
} from '@/components/ui/form';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import { toast } from 'sonner';
import { handleError, handleSuccess, AppError } from '@/lib/errorHandler';
import { logger } from '@/lib/logger';
import { supabase } from '@/integrations/supabase/client';
import { Loader2, Mail, CheckCircle2, AlertCircle } from 'lucide-react';
import { TurnstileCaptcha } from '@/components/auth/TurnstileCaptcha';
@@ -79,16 +80,18 @@ export function EmailChangeDialog({ open, onOpenChange, currentEmail, userId }:
const onSubmit = async (data: EmailFormData) => {
if (!captchaToken) {
toast.error('CAPTCHA Required', {
description: 'Please complete the CAPTCHA verification.',
});
handleError(
new AppError('Please complete the CAPTCHA verification.', 'CAPTCHA_REQUIRED'),
{ action: 'Change email', userId, metadata: { step: 'captcha_validation' } }
);
return;
}
if (data.newEmail.toLowerCase() === currentEmail.toLowerCase()) {
toast.error('Same Email', {
description: 'The new email is the same as your current email.',
});
handleError(
new AppError('The new email is the same as your current email.', 'SAME_EMAIL'),
{ action: 'Change email', userId, metadata: { currentEmail, newEmail: data.newEmail } }
);
return;
}
@@ -98,9 +101,13 @@ export function EmailChangeDialog({ open, onOpenChange, currentEmail, userId }:
const emailValidation = await validateEmailNotDisposable(data.newEmail);
if (!emailValidation.valid) {
toast.error("Invalid Email", {
description: emailValidation.reason || "Please use a permanent email address"
});
handleError(
new AppError(
emailValidation.reason || "Please use a permanent email address",
'DISPOSABLE_EMAIL'
),
{ action: 'Validate email', userId, metadata: { email: data.newEmail } }
);
setLoading(false);
return;
}
@@ -143,7 +150,13 @@ export function EmailChangeDialog({ open, onOpenChange, currentEmail, userId }:
timestamp: new Date().toISOString(),
}
}).then(({ error }) => {
if (error) console.error('Failed to log email change:', error);
if (error) {
logger.error('Failed to log email change', {
userId,
action: 'email_change_audit_log',
error: error.message
});
}
});
// Step 6: Send security notifications (non-blocking)
@@ -158,27 +171,60 @@ export function EmailChangeDialog({ open, onOpenChange, currentEmail, userId }:
timestamp: new Date().toISOString(),
}
}).catch(error => {
console.error('Failed to send security notification:', error);
logger.error('Failed to send security notification', {
userId,
action: 'email_change_notification',
error: error instanceof Error ? error.message : String(error)
});
});
}
toast.success('Email change initiated', {
description: 'Check both email addresses for confirmation links.',
});
handleSuccess(
'Email change initiated',
'Check both email addresses for confirmation links.'
);
setStep('success');
} catch (error: any) {
console.error('Email change error:', error);
// Handle rate limiting specifically
logger.error('Email change failed', {
userId,
action: 'email_change',
error: error instanceof Error ? error.message : String(error),
errorCode: error.code,
errorStatus: error.status
});
if (error.message?.includes('rate limit') || error.status === 429) {
toast.error('Too Many Attempts', {
description: 'Please wait a few minutes before trying again.',
});
handleError(
new AppError(
'Please wait a few minutes before trying again.',
'RATE_LIMIT',
'Too many email change attempts'
),
{ action: 'Change email', userId, metadata: { currentEmail, newEmail: data.newEmail } }
);
} else if (error.message?.includes('Invalid login credentials')) {
handleError(
new AppError(
'The password you entered is incorrect.',
'INVALID_PASSWORD',
'Incorrect password during email change'
),
{ action: 'Verify password', userId }
);
} else {
toast.error('Failed to change email', {
description: error.message || 'Please try again.',
});
handleError(
error,
{
action: 'Change email',
userId,
metadata: {
currentEmail,
newEmail: data.newEmail,
errorType: error.constructor.name
}
}
);
}
} finally {
setLoading(false);