mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 06:11:11 -05:00
feat: Implement comprehensive ban enforcement
This commit is contained in:
82
src/hooks/useBanCheck.ts
Normal file
82
src/hooks/useBanCheck.ts
Normal 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 };
|
||||
}
|
||||
Reference in New Issue
Block a user