mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 00:51:14 -05:00
Add moderation queue tables
This commit is contained in:
54
src/hooks/useUserRole.ts
Normal file
54
src/hooks/useUserRole.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { useAuth } from '@/hooks/useAuth';
|
||||
|
||||
export type UserRole = 'admin' | 'moderator' | 'user';
|
||||
|
||||
export function useUserRole() {
|
||||
const { user } = useAuth();
|
||||
const [roles, setRoles] = useState<UserRole[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
if (!user) {
|
||||
setRoles([]);
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const fetchRoles = async () => {
|
||||
try {
|
||||
const { data, error } = await supabase
|
||||
.from('user_roles')
|
||||
.select('role')
|
||||
.eq('user_id', user.id);
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching user roles:', error);
|
||||
setRoles([]);
|
||||
} else {
|
||||
setRoles(data?.map(r => r.role as UserRole) || []);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching user roles:', error);
|
||||
setRoles([]);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchRoles();
|
||||
}, [user]);
|
||||
|
||||
const hasRole = (role: UserRole) => roles.includes(role);
|
||||
const isModerator = () => hasRole('admin') || hasRole('moderator');
|
||||
const isAdmin = () => hasRole('admin');
|
||||
|
||||
return {
|
||||
roles,
|
||||
loading,
|
||||
hasRole,
|
||||
isModerator,
|
||||
isAdmin
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user