πŸ“„ SKILL.md

← Vault

name: comercialrs-adm-role-debug

description: Debug and fix /adm access and role-based access in ComercialRS β€” when AdmGuard redirects to / despite user having admin role in profiles table

tags:

- supabase

- rls

- react

- comercialrs

- auth


Debugging /adm Access in ComercialRS

Trigger Conditions


Bug 4: RLS Blocking Writes on user_roles (Role Change Fails Silently)

Symptom

In /adm β†’ UsuΓ‘rios & Cargos, changing a user's role via the Select dropdown shows error toast "Falha ao atualizar cargo". The role does NOT persist after refresh.

Root Cause

Table user_roles had RLS enabled but only a SELECT policy existed. The frontend code does DELETE + INSERT to change roles β€” INSERT and DELETE operations were blocked by RLS.

`tsx

// AdmUsers.tsx β€” changeRole does delete + insert

const { error: delErr } = await supabase.from('user_roles').delete().eq('user_id', userId);

const { error: insErr } = await supabase.from('user_roles').insert({ user_id: userId, role: newRole });

`

Diagnostic

Check existing RLS policies on user_roles:

`bash

curl -s -X POST "https://api.supabase.com/v1/projects//database/query" \

-H "Authorization: Bearer $PAT" \

-d '{"query": "SELECT policyname, cmd FROM pg_policies WHERE tablename = '\''user_roles'\''"}'

`

Expected: SELECT, INSERT, DELETE, UPDATE policies. If only SELECT exists, writes are blocked.

Also test with anon key β€” empty [] on SELECT doesn't mean table is empty (RLS blocks it):

`bash

curl -s "https://dauftiqcvgaydddoxqhh.supabase.co/rest/v1/user_roles?limit=5" \

-H "apikey: " -H "Authorization: Bearer "

`

Fix

Add missing INSERT, UPDATE, DELETE policies via Management API:

`bash

PAT=""

PROJECT="dauftiqcvgaydddoxqhh"

curl -s -X POST "https://api.supabase.com/v1/projects/$PROJECT/database/query" \

-H "Authorization: Bearer $PAT" -H "Content-Type: application/json" \

-d '{"query": "ALTER TABLE user_roles ENABLE ROW LEVEL SECURITY"}'

curl -s -X POST "https://api.supabase.com/v1/projects/$PROJECT/database/query" \

-H "Authorization: Bearer $PAT" -H "Content-Type: application/json" \

-d '{"query": "CREATE POLICY user_roles_insert ON public.user_roles FOR INSERT TO authenticated WITH CHECK (true)"}'

curl -s -X POST "https://api.supabase.com/v1/projects/$PROJECT/database/query" \

-H "Authorization: Bearer $PAT" -H "Content-Type: application/json" \

-d '{"query": "CREATE POLICY user_roles_delete ON public.user_roles FOR DELETE TO authenticated USING (true)"}'

curl -s -X POST "https://api.supabase.com/v1/projects/$PROJECT/database/query" \

-H "Authorization: Bearer $PAT" -H "Content-Type: application/json" \

-d '{"query": "CREATE POLICY user_roles_update ON public.user_roles FOR UPDATE TO authenticated USING (true) WITH CHECK (true)"}'

`

Verification

After applying policies, test role change in /adm β†’ UsuΓ‘rios & Cargos. Should show "Cargo atualizado" toast and persist after page refresh.

Also verify data is readable:

`bash

curl -s -X POST "https://api.supabase.com/v1/projects/$PROJECT/database/query" \

-H "Authorization: Bearer $PAT" -H "Content-Type: application/json" \

-d '{"query": "SELECT user_id, role FROM user_roles LIMIT 3"}'

`

Should return rows (not empty) β€” user_roles contains IDs from Trindade Painel do Corretor system, not Supabase auth IDs.