mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-25 02:11:12 -05:00
Enable RLS on rate limits table
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useState } from 'react';
|
||||
import { useReducer } from 'react';
|
||||
import { AlertDialog, AlertDialogContent, AlertDialogHeader, AlertDialogTitle, AlertDialogDescription, AlertDialogFooter } from '@/components/ui/alert-dialog';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
@@ -6,8 +6,15 @@ import { Label } from '@/components/ui/label';
|
||||
import { Checkbox } from '@/components/ui/checkbox';
|
||||
import { Alert, AlertDescription } from '@/components/ui/alert';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { useToast } from '@/hooks/use-toast';
|
||||
import { Loader2, AlertTriangle, Info } from 'lucide-react';
|
||||
import {
|
||||
deletionDialogReducer,
|
||||
initialState,
|
||||
canProceedToConfirm,
|
||||
canRequestDeletion,
|
||||
canConfirmDeletion
|
||||
} from '@/lib/deletionDialogMachine';
|
||||
import { handleError, handleSuccess } from '@/lib/errorHandler';
|
||||
|
||||
interface AccountDeletionDialogProps {
|
||||
open: boolean;
|
||||
@@ -17,15 +24,13 @@ interface AccountDeletionDialogProps {
|
||||
}
|
||||
|
||||
export function AccountDeletionDialog({ open, onOpenChange, userEmail, onDeletionRequested }: AccountDeletionDialogProps) {
|
||||
const [step, setStep] = useState<'warning' | 'confirm' | 'code'>('warning');
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [confirmationCode, setConfirmationCode] = useState('');
|
||||
const [codeReceived, setCodeReceived] = useState(false);
|
||||
const [scheduledDate, setScheduledDate] = useState<string>('');
|
||||
const { toast } = useToast();
|
||||
const [state, dispatch] = useReducer(deletionDialogReducer, initialState);
|
||||
|
||||
const handleRequestDeletion = async () => {
|
||||
setLoading(true);
|
||||
if (!canRequestDeletion(state)) return;
|
||||
|
||||
dispatch({ type: 'SET_LOADING', payload: true });
|
||||
|
||||
try {
|
||||
const { data, error } = await supabase.functions.invoke('request-account-deletion', {
|
||||
body: {},
|
||||
@@ -33,63 +38,52 @@ export function AccountDeletionDialog({ open, onOpenChange, userEmail, onDeletio
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
setScheduledDate(data.scheduled_deletion_at);
|
||||
setStep('code');
|
||||
dispatch({
|
||||
type: 'REQUEST_DELETION',
|
||||
payload: { scheduledDate: data.scheduled_deletion_at }
|
||||
});
|
||||
onDeletionRequested();
|
||||
|
||||
toast({
|
||||
title: 'Deletion Requested',
|
||||
description: 'Check your email for the confirmation code.',
|
||||
});
|
||||
} catch (error: any) {
|
||||
toast({
|
||||
variant: 'destructive',
|
||||
title: 'Error',
|
||||
description: error.message || 'Failed to request account deletion',
|
||||
});
|
||||
} finally {
|
||||
setLoading(false);
|
||||
handleSuccess('Deletion Requested', 'Check your email for the confirmation code.');
|
||||
} catch (error) {
|
||||
handleError(error, { action: 'Request account deletion' });
|
||||
dispatch({ type: 'SET_ERROR', payload: 'Failed to request deletion' });
|
||||
}
|
||||
};
|
||||
|
||||
const handleConfirmDeletion = async () => {
|
||||
if (!confirmationCode || confirmationCode.length !== 6) {
|
||||
toast({
|
||||
variant: 'destructive',
|
||||
title: 'Invalid Code',
|
||||
description: 'Please enter a 6-digit confirmation code',
|
||||
});
|
||||
if (!canConfirmDeletion(state)) {
|
||||
handleError(
|
||||
new Error('Please enter a 6-digit confirmation code and confirm you received it'),
|
||||
{ action: 'Confirm deletion' }
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
setLoading(true);
|
||||
dispatch({ type: 'SET_LOADING', payload: true });
|
||||
|
||||
try {
|
||||
const { error } = await supabase.functions.invoke('confirm-account-deletion', {
|
||||
body: { confirmation_code: confirmationCode },
|
||||
body: { confirmation_code: state.confirmationCode },
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
toast({
|
||||
title: 'Deletion Confirmed',
|
||||
description: 'Your account has been deactivated and scheduled for permanent deletion.',
|
||||
});
|
||||
handleSuccess(
|
||||
'Deletion Confirmed',
|
||||
'Your account has been deactivated and scheduled for permanent deletion.'
|
||||
);
|
||||
|
||||
// Refresh the page to show the deletion banner
|
||||
window.location.reload();
|
||||
} catch (error: any) {
|
||||
toast({
|
||||
variant: 'destructive',
|
||||
title: 'Error',
|
||||
description: error.message || 'Failed to confirm deletion',
|
||||
});
|
||||
} finally {
|
||||
setLoading(false);
|
||||
} catch (error) {
|
||||
handleError(error, { action: 'Confirm account deletion' });
|
||||
}
|
||||
};
|
||||
|
||||
const handleResendCode = async () => {
|
||||
setLoading(true);
|
||||
dispatch({ type: 'SET_LOADING', payload: true });
|
||||
|
||||
try {
|
||||
const { error } = await supabase.functions.invoke('resend-deletion-code', {
|
||||
body: {},
|
||||
@@ -97,25 +91,16 @@ export function AccountDeletionDialog({ open, onOpenChange, userEmail, onDeletio
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
toast({
|
||||
title: 'Code Resent',
|
||||
description: 'A new confirmation code has been sent to your email.',
|
||||
});
|
||||
} catch (error: any) {
|
||||
toast({
|
||||
variant: 'destructive',
|
||||
title: 'Error',
|
||||
description: error.message || 'Failed to resend code',
|
||||
});
|
||||
handleSuccess('Code Resent', 'A new confirmation code has been sent to your email.');
|
||||
} catch (error) {
|
||||
handleError(error, { action: 'Resend deletion code' });
|
||||
} finally {
|
||||
setLoading(false);
|
||||
dispatch({ type: 'SET_LOADING', payload: false });
|
||||
}
|
||||
};
|
||||
|
||||
const handleClose = () => {
|
||||
setStep('warning');
|
||||
setConfirmationCode('');
|
||||
setCodeReceived(false);
|
||||
dispatch({ type: 'RESET' });
|
||||
onOpenChange(false);
|
||||
};
|
||||
|
||||
@@ -128,7 +113,7 @@ export function AccountDeletionDialog({ open, onOpenChange, userEmail, onDeletio
|
||||
Delete Account
|
||||
</AlertDialogTitle>
|
||||
<AlertDialogDescription className="text-left space-y-4">
|
||||
{step === 'warning' && (
|
||||
{state.step === 'warning' && (
|
||||
<>
|
||||
<Alert>
|
||||
<Info className="w-4 h-4" />
|
||||
@@ -165,7 +150,7 @@ export function AccountDeletionDialog({ open, onOpenChange, userEmail, onDeletio
|
||||
</>
|
||||
)}
|
||||
|
||||
{step === 'confirm' && (
|
||||
{state.step === 'confirm' && (
|
||||
<Alert variant="destructive">
|
||||
<AlertDescription>
|
||||
Are you absolutely sure? You'll receive a confirmation code via email. After confirming with the code, your account will be deactivated and scheduled for deletion in 2 weeks.
|
||||
@@ -173,13 +158,13 @@ export function AccountDeletionDialog({ open, onOpenChange, userEmail, onDeletio
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
{step === 'code' && (
|
||||
{state.step === 'code' && (
|
||||
<div className="space-y-4">
|
||||
<Alert>
|
||||
<Info className="w-4 h-4" />
|
||||
<AlertDescription>
|
||||
A 6-digit confirmation code has been sent to <strong>{userEmail}</strong>. Enter it below within 24 hours to confirm deletion. Your account will be deactivated and scheduled for deletion on{' '}
|
||||
<strong>{scheduledDate ? new Date(scheduledDate).toLocaleDateString() : '14 days from confirmation'}</strong>.
|
||||
<strong>{state.scheduledDate ? new Date(state.scheduledDate).toLocaleDateString() : '14 days from confirmation'}</strong>.
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
|
||||
@@ -191,8 +176,8 @@ export function AccountDeletionDialog({ open, onOpenChange, userEmail, onDeletio
|
||||
type="text"
|
||||
placeholder="000000"
|
||||
maxLength={6}
|
||||
value={confirmationCode}
|
||||
onChange={(e) => setConfirmationCode(e.target.value.replace(/\D/g, ''))}
|
||||
value={state.confirmationCode}
|
||||
onChange={(e) => dispatch({ type: 'UPDATE_CODE', payload: { code: e.target.value } })}
|
||||
className="text-center text-2xl tracking-widest"
|
||||
/>
|
||||
</div>
|
||||
@@ -200,8 +185,8 @@ export function AccountDeletionDialog({ open, onOpenChange, userEmail, onDeletio
|
||||
<div className="flex items-center space-x-2">
|
||||
<Checkbox
|
||||
id="codeReceived"
|
||||
checked={codeReceived}
|
||||
onCheckedChange={(checked) => setCodeReceived(checked === true)}
|
||||
checked={state.codeReceived}
|
||||
onCheckedChange={() => dispatch({ type: 'TOGGLE_CODE_RECEIVED' })}
|
||||
/>
|
||||
<Label htmlFor="codeReceived" className="text-sm font-normal cursor-pointer">
|
||||
I have received the code in my email
|
||||
@@ -211,10 +196,10 @@ export function AccountDeletionDialog({ open, onOpenChange, userEmail, onDeletio
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={handleResendCode}
|
||||
disabled={loading}
|
||||
disabled={state.isLoading}
|
||||
className="w-full"
|
||||
>
|
||||
{loading ? <Loader2 className="w-4 h-4 animate-spin" /> : 'Resend Code'}
|
||||
{state.isLoading ? <Loader2 className="w-4 h-4 animate-spin" /> : 'Resend Code'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -222,34 +207,38 @@ export function AccountDeletionDialog({ open, onOpenChange, userEmail, onDeletio
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
{step === 'warning' && (
|
||||
{state.step === 'warning' && (
|
||||
<>
|
||||
<Button variant="outline" onClick={handleClose}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button variant="destructive" onClick={() => setStep('confirm')}>
|
||||
<Button
|
||||
variant="destructive"
|
||||
onClick={() => dispatch({ type: 'CONTINUE_TO_CONFIRM' })}
|
||||
disabled={!canProceedToConfirm(state)}
|
||||
>
|
||||
Continue
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
|
||||
{step === 'confirm' && (
|
||||
{state.step === 'confirm' && (
|
||||
<>
|
||||
<Button variant="outline" onClick={() => setStep('warning')}>
|
||||
<Button variant="outline" onClick={() => dispatch({ type: 'GO_BACK_TO_WARNING' })}>
|
||||
Go Back
|
||||
</Button>
|
||||
<Button
|
||||
variant="destructive"
|
||||
onClick={handleRequestDeletion}
|
||||
disabled={loading}
|
||||
disabled={!canRequestDeletion(state)}
|
||||
>
|
||||
{loading ? <Loader2 className="w-4 h-4 animate-spin mr-2" /> : null}
|
||||
{state.isLoading ? <Loader2 className="w-4 h-4 animate-spin mr-2" /> : null}
|
||||
Request Deletion
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
|
||||
{step === 'code' && (
|
||||
{state.step === 'code' && (
|
||||
<>
|
||||
<Button variant="outline" onClick={handleClose}>
|
||||
Close
|
||||
@@ -257,9 +246,9 @@ export function AccountDeletionDialog({ open, onOpenChange, userEmail, onDeletio
|
||||
<Button
|
||||
variant="destructive"
|
||||
onClick={handleConfirmDeletion}
|
||||
disabled={loading || !codeReceived || confirmationCode.length !== 6}
|
||||
disabled={!canConfirmDeletion(state)}
|
||||
>
|
||||
{loading ? <Loader2 className="w-4 h-4 animate-spin mr-2" /> : null}
|
||||
{state.isLoading ? <Loader2 className="w-4 h-4 animate-spin mr-2" /> : null}
|
||||
Verify Code & Deactivate Account
|
||||
</Button>
|
||||
</>
|
||||
|
||||
Reference in New Issue
Block a user