mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 12:31:12 -05:00
Refactor EmailChangeDialog error handling
This commit is contained in:
@@ -20,7 +20,8 @@ import {
|
|||||||
} from '@/components/ui/form';
|
} from '@/components/ui/form';
|
||||||
import { Input } from '@/components/ui/input';
|
import { Input } from '@/components/ui/input';
|
||||||
import { Button } from '@/components/ui/button';
|
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 { supabase } from '@/integrations/supabase/client';
|
||||||
import { Loader2, Mail, CheckCircle2, AlertCircle } from 'lucide-react';
|
import { Loader2, Mail, CheckCircle2, AlertCircle } from 'lucide-react';
|
||||||
import { TurnstileCaptcha } from '@/components/auth/TurnstileCaptcha';
|
import { TurnstileCaptcha } from '@/components/auth/TurnstileCaptcha';
|
||||||
@@ -79,16 +80,18 @@ export function EmailChangeDialog({ open, onOpenChange, currentEmail, userId }:
|
|||||||
|
|
||||||
const onSubmit = async (data: EmailFormData) => {
|
const onSubmit = async (data: EmailFormData) => {
|
||||||
if (!captchaToken) {
|
if (!captchaToken) {
|
||||||
toast.error('CAPTCHA Required', {
|
handleError(
|
||||||
description: 'Please complete the CAPTCHA verification.',
|
new AppError('Please complete the CAPTCHA verification.', 'CAPTCHA_REQUIRED'),
|
||||||
});
|
{ action: 'Change email', userId, metadata: { step: 'captcha_validation' } }
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.newEmail.toLowerCase() === currentEmail.toLowerCase()) {
|
if (data.newEmail.toLowerCase() === currentEmail.toLowerCase()) {
|
||||||
toast.error('Same Email', {
|
handleError(
|
||||||
description: 'The new email is the same as your current email.',
|
new AppError('The new email is the same as your current email.', 'SAME_EMAIL'),
|
||||||
});
|
{ action: 'Change email', userId, metadata: { currentEmail, newEmail: data.newEmail } }
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,9 +101,13 @@ export function EmailChangeDialog({ open, onOpenChange, currentEmail, userId }:
|
|||||||
const emailValidation = await validateEmailNotDisposable(data.newEmail);
|
const emailValidation = await validateEmailNotDisposable(data.newEmail);
|
||||||
|
|
||||||
if (!emailValidation.valid) {
|
if (!emailValidation.valid) {
|
||||||
toast.error("Invalid Email", {
|
handleError(
|
||||||
description: emailValidation.reason || "Please use a permanent email address"
|
new AppError(
|
||||||
});
|
emailValidation.reason || "Please use a permanent email address",
|
||||||
|
'DISPOSABLE_EMAIL'
|
||||||
|
),
|
||||||
|
{ action: 'Validate email', userId, metadata: { email: data.newEmail } }
|
||||||
|
);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -143,7 +150,13 @@ export function EmailChangeDialog({ open, onOpenChange, currentEmail, userId }:
|
|||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
}
|
}
|
||||||
}).then(({ error }) => {
|
}).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)
|
// Step 6: Send security notifications (non-blocking)
|
||||||
@@ -158,27 +171,60 @@ export function EmailChangeDialog({ open, onOpenChange, currentEmail, userId }:
|
|||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
}
|
}
|
||||||
}).catch(error => {
|
}).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', {
|
handleSuccess(
|
||||||
description: 'Check both email addresses for confirmation links.',
|
'Email change initiated',
|
||||||
});
|
'Check both email addresses for confirmation links.'
|
||||||
|
);
|
||||||
|
|
||||||
setStep('success');
|
setStep('success');
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('Email change error:', error);
|
logger.error('Email change failed', {
|
||||||
|
userId,
|
||||||
// Handle rate limiting specifically
|
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) {
|
if (error.message?.includes('rate limit') || error.status === 429) {
|
||||||
toast.error('Too Many Attempts', {
|
handleError(
|
||||||
description: 'Please wait a few minutes before trying again.',
|
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 {
|
} else {
|
||||||
toast.error('Failed to change email', {
|
handleError(
|
||||||
description: error.message || 'Please try again.',
|
error,
|
||||||
});
|
{
|
||||||
|
action: 'Change email',
|
||||||
|
userId,
|
||||||
|
metadata: {
|
||||||
|
currentEmail,
|
||||||
|
newEmail: data.newEmail,
|
||||||
|
errorType: error.constructor.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
|||||||
Reference in New Issue
Block a user