From be5e86773b9cdaf9d8926666cf77f9dedcd9f36f Mon Sep 17 00:00:00 2001 From: Marcos Reifonas Date: Wed, 19 Aug 2026 20:43:38 +0000 Subject: [PATCH] fix(auth): replace .maybeSingle() with .limit(1) to avoid 406 Not Acceptable errors on PostgREST for missing resources --- src/components/dashboard/UserInfo.tsx | 8 ++++---- src/hooks/useInterfaceResources.tsx | 7 ++++--- src/hooks/useUserResourcePermissions.tsx | 12 +++++++----- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/components/dashboard/UserInfo.tsx b/src/components/dashboard/UserInfo.tsx index b2fa6cb..d8470d2 100644 --- a/src/components/dashboard/UserInfo.tsx +++ b/src/components/dashboard/UserInfo.tsx @@ -46,14 +46,14 @@ export function UserInfo() { useEffect(() => { async function fetchUserProfile() { if (!user) return; - const { data, error } = await supabase + const { data: profileArr, error } = await supabase .from('profiles') .select('full_name, email, profile_image_url') .eq('id', user.id) - .maybeSingle(); + .limit(1); - if (!error && data) { - setProfile(data); + if (!error && profileArr && profileArr.length > 0) { + setProfile(profileArr[0]); } else { setProfile({ // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/src/hooks/useInterfaceResources.tsx b/src/hooks/useInterfaceResources.tsx index 1cdcd1c..3d31b1e 100644 --- a/src/hooks/useInterfaceResources.tsx +++ b/src/hooks/useInterfaceResources.tsx @@ -209,16 +209,17 @@ export function useInterfaceResources() { .from('profiles') .select('privilege_id') .eq('id', userId) - .maybeSingle(); + .limit(1); if (profileError) throw profileError; - if (!profile?.privilege_id) return []; + const priv = profile && profile.length > 0 ? profile[0].privilege_id : null; + if (!priv) return []; const { data, error } = await supabase .from('privilege_interface_resources') .select('resource_key') - .eq('privilege_id', profile.privilege_id); + .eq('privilege_id', priv); if (error) throw error; diff --git a/src/hooks/useUserResourcePermissions.tsx b/src/hooks/useUserResourcePermissions.tsx index 8de41f7..ea64a5f 100644 --- a/src/hooks/useUserResourcePermissions.tsx +++ b/src/hooks/useUserResourcePermissions.tsx @@ -67,19 +67,20 @@ export function useUserResourcePermissions(resourceKey: string) { // Enriquecer com dados do usuário const enrichedData = await Promise.all( (data || []).map(async (permission) => { - const { data: profileData } = await supabase + const { data: profileDataArr } = await supabase .from('profiles') .select('email, full_name') .eq('id', permission.user_id) - .maybeSingle(); + .limit(1); + const profileData = profileDataArr && profileDataArr.length > 0 ? profileDataArr[0] : null; return { user_id: permission.user_id, resource_key: permission.resource_key, permission_level: permission.permission as PermissionLevel, created_at: permission.created_at, updated_at: permission.updated_at, - profiles: profileData + profiles: profileData as any } as UserInterfacePermission; }) ); @@ -167,12 +168,13 @@ export function useUserResourcePermissions(resourceKey: string) { } // Verify user exists - const { data: userExists, error: userError } = await supabase + const { data: userExistsArr, error: userError } = await supabase .from('profiles') .select('id, email') .eq('id', userId) - .maybeSingle(); + .limit(1); + const userExists = userExistsArr && userExistsArr.length > 0 ? userExistsArr[0] : null; if (userError || !userExists) { console.error('User not found:', { userId, userError }); toast.error('Usuário não encontrado');