feat: Implement comprehensive ban enforcement

This commit is contained in:
gpt-engineer-app[bot]
2025-10-30 01:47:51 +00:00
parent f95eaf9eb7
commit fe76c8c572
8 changed files with 452 additions and 0 deletions

82
src/hooks/useBanCheck.ts Normal file
View File

@@ -0,0 +1,82 @@
import { useEffect, useState } from 'react';
import { useAuth } from '@/hooks/useAuth';
import { supabase } from '@/integrations/supabase/client';
import { useNavigate } from 'react-router-dom';
import { toast } from '@/hooks/use-toast';
export function useBanCheck() {
const { user } = useAuth();
const navigate = useNavigate();
const [isBanned, setIsBanned] = useState(false);
const [loading, setLoading] = useState(true);
useEffect(() => {
if (!user) {
setIsBanned(false);
setLoading(false);
return;
}
const checkBan = async () => {
try {
const { data: profile } = await supabase
.from('profiles')
.select('banned')
.eq('user_id', user.id)
.single();
if (profile?.banned) {
setIsBanned(true);
toast({
title: 'Account Suspended',
description: 'Your account has been suspended. Contact support for assistance.',
variant: 'destructive',
duration: Infinity // Don't auto-dismiss
});
// Sign out banned user
await supabase.auth.signOut();
navigate('/');
}
} catch (error) {
console.error('Ban check error:', error);
} finally {
setLoading(false);
}
};
checkBan();
// Subscribe to profile changes (real-time ban detection)
const channel = supabase
.channel('ban-check')
.on(
'postgres_changes',
{
event: 'UPDATE',
schema: 'public',
table: 'profiles',
filter: `user_id=eq.${user.id}`
},
(payload) => {
if (payload.new && (payload.new as { banned: boolean }).banned) {
setIsBanned(true);
toast({
title: 'Account Suspended',
description: 'Your account has been suspended. Contact support for assistance.',
variant: 'destructive',
duration: Infinity
});
supabase.auth.signOut();
navigate('/');
}
}
)
.subscribe();
return () => {
supabase.removeChannel(channel);
};
}, [user, navigate]);
return { isBanned, loading };
}