mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 13:51:13 -05:00
Fix edge function role check
This commit is contained in:
@@ -88,21 +88,25 @@ serve(async (req) => {
|
||||
);
|
||||
|
||||
// Check if user has moderator permissions using service role to bypass RLS
|
||||
const { data: profile, error: profileError } = await supabase
|
||||
.from('profiles')
|
||||
const { data: roles, error: rolesError } = await supabase
|
||||
.from('user_roles')
|
||||
.select('role')
|
||||
.eq('user_id', authenticatedUserId)
|
||||
.single();
|
||||
.eq('user_id', authenticatedUserId);
|
||||
|
||||
if (profileError || !profile) {
|
||||
console.error('Failed to fetch profile:', profileError);
|
||||
if (rolesError) {
|
||||
console.error('Failed to fetch user roles:', rolesError);
|
||||
return new Response(
|
||||
JSON.stringify({ error: 'User profile not found.' }),
|
||||
JSON.stringify({ error: 'Failed to verify user permissions.' }),
|
||||
{ status: 403, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
||||
);
|
||||
}
|
||||
|
||||
if (profile.role !== 'moderator' && profile.role !== 'admin') {
|
||||
const userRoles = roles?.map(r => r.role) || [];
|
||||
const isModerator = userRoles.includes('moderator') ||
|
||||
userRoles.includes('admin') ||
|
||||
userRoles.includes('superuser');
|
||||
|
||||
if (!isModerator) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: 'Insufficient permissions. Moderator role required.' }),
|
||||
{ status: 403, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
||||
|
||||
Reference in New Issue
Block a user