Backend · Skill

Add user roles and permissions

add-user-roles-and-permissions

Adds admin and member roles on top of sign-in you already have, and enforces them with a Postgres policy or request middleware. Use this when the user wants an admin area, member versus admin access, permission checks, or to stop a member running an admin action.

Works with: Any AI Not yet model-tested; testing Jan 2027

Paste it into ChatGPT, Claude, Gemini or any AI chat, then tell it about your project. How it works

Install in Claude Code, Cursor or Codex (optional)

Only for AI coding tools that read skill folders. Run one line in a terminal, in your project folder. It downloads this skill's text files from TheSkillVault and nothing else; you can read them first with “View raw SKILL.md”.

Claude Code (all your projects)

curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/add-user-roles-and-permissions/raw' -o ~/'.claude/skills/add-user-roles-and-permissions/SKILL.md'

Cursor (this project)

curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/add-user-roles-and-permissions/raw' -o '.cursor/skills/add-user-roles-and-permissions/SKILL.md'

Codex and other Agent Skills tools (this project)

curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/add-user-roles-and-permissions/raw' -o '.agents/skills/add-user-roles-and-permissions/SKILL.md'

Or connect the whole library over MCP

Add TheSkillVault to Cursor or add this to your app's MCP settings (read-only; help):

{
  "mcpServers": {
    "theskillvault": {
      "url": "https://theskillvault.ai/mcp"
    }
  }
}

Sign-in answers who the person is. This skill answers what that person may do. Keep the list short: member by default, admin for the extra actions.

If nobody can sign in yet, stop and use set-up-supabase-auth, add-clerk-auth, or design-sessions-and-permissions. Come back here for the role check. For general table policies and indexes, use design-supabase-postgres-schema.

When to use

  • You need an admin area that members must not open.
  • A member must not change their own role to admin.
  • You want the check in Postgres row policies, or in server middleware.

When not to use

  • You are still choosing sessions, tokens or a password hash. That is the sessions skill.
  • You only need "this row belongs to this person", with no admin override. An owner check is enough.

Steps

  1. Write a short table of actions. Members can read and edit their own rows. Admins can do the extra ones you name, such as refund, invite or edit someone else. Do not invent actions you will not enforce.
  2. Store role on the profile row: text not null default 'member', with a check that it is only admin or member. Seed the first admin in SQL or the dashboard.
  3. Ignore any role sent by the browser at sign-up. The default is member.
  4. Postgres path: put a helper in a private schema that returns the signed-in user's role, with a fixed search_path. Policies call that helper. Admin writes use it in both using and with check.
  5. The profile update policy must refuse a role change unless the caller is already an admin. A member can change their display name and must not change role.
  6. Middleware path: on every admin request, load the role from the database on the server. Return 401 when there is no signed-in user. Return 403 when the role is not admin. Hiding a link is not the check.
  7. If you use Clerk, read the role from the server session claims and map it to admin or member. If you use Supabase, prefer the row policy so the public key cannot bypass it. Do not keep two sources that can disagree.
  8. Test with two accounts. The member is rejected. The admin is allowed. A member's direct API call is rejected too.
export function requireAdmin(user: { role: string } | null) {
  if (!user) return new Response("Sign in required", { status: 401 });
  if (user.role !== "admin") return new Response("Admins only", { status: 403 });
  return null;
}
create schema if not exists private;

create or replace function private.current_role()
returns text language sql stable security definer set search_path = ''
as $$ select role from public.profiles where id = (select auth.uid()); $$;

revoke all on function private.current_role() from public, anon;
grant execute on function private.current_role() to authenticated;

create policy "admins update orders" on public.orders
for update to authenticated
using ((select private.current_role()) = 'admin')
with check ((select private.current_role()) = 'admin');

Gotchas

  • A role stored only in a cookie or in local storage can be edited by the user. Read it from the database or from a claim your server set.
  • The Supabase secret key bypasses row policies. Admin screens that use it must still check the role in code.
  • Do not add the private schema to the exposed API schemas. The helper is not a public RPC.
  • One shared "staff" login cannot be revoked for a single person. Give each admin their own account.
  • Adding a third role later is a migration plus a policy change. Do it on purpose, not by sprinkling string checks.

Safety

  • Never accept role from form data or query parameters. That is a privilege upgrade.
  • Keep the database secret and any service credential in the server environment, not in the client bundle.

Portability

Works with any framework. If you use a different database instead of Postgres, keep the same rules in middleware: load the role on the server, 401 when signed out, 403 when the role is wrong. The policy version is the Postgres form of that check.

Example prompt

"We already have sign-in. Add member and admin roles so only admins can open refunds, and members cannot promote themselves."

Done when

  • New accounts are members, and only an existing admin can grant admin.
  • A member's request to an admin action returns 403, including a hand-written API call.
  • The public data API cannot update the role column.

---

House original by TheSkillVault (CC-BY-4.0).

Licence

Licence: CC BY 4.0. Attribution when you reuse it: From TheSkillVault, https://theskillvault.ai, CC BY 4.0