agentleFS
charanjit-singhcharanjit-singh/lovable-skills/skills/supabase-rls-and-auth/SKILL.md

supabase-rls-and-auth

Skill10 starsChanged 6 months agoMITView on GitHub

Use when creating Supabase tables, row level security policies, multi-tenant data isolation, auth-gated routes, or fixing users seeing wrong data. Not for styling, forms validation only, or non-Supabase auth providers unless wiring sessions to Supabase.

The file

We split this skill into the rules it gives an agent

6 rules. About 0 are common, 3 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Every user-owned table must have RLS enabled and policies before shipping.

    line 14
  2. Very specific:

    Use WITH CHECK on insert/update mirroring the same rule.

    line 50
  3. Distinctive:

    Use Supabase Auth session.

    line 54
  4. Very specific:

    Read auth.uid() in policies.

    line 55
  5. Very specific:

    Use (select auth.uid()) inside policies (with the parentheses) so Postgres caches the value per statement instead of per row.

    line 62
  6. Distinctive:

    Ask the user to test with two accounts: user A must not read/write user B's rows.

    line 74

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Supabase RLS and auth

For sign-in flows and OAuth, use supabase-auth-flows. This skill covers data access policies.

Every user-owned table must have RLS enabled and policies before shipping. Never rely on hiding UI alone.

New table checklist

  • ALTER TABLE ... ENABLE ROW LEVEL SECURITY;
  • Policy for SELECT — users read only their rows (or org rows).
  • Policy for INSERT — auth.uid() matches user_id (or membership check).
  • Policy for UPDATE / DELETE — same ownership rule.
  • user_id (or organization_id) column with FK to auth.users where applicable.
  • Service-role key never in client code.

Policy patterns

Single-user ownership

CREATE POLICY "users_select_own" ON public.posts
  FOR SELECT USING (auth.uid() = user_id);

CREATE POLICY "users_insert_own" ON public.posts
  FOR INSERT WITH CHECK (auth.uid() = user_id);

Organization / tenant

CREATE POLICY "members_select" ON public.projects
  FOR SELECT USING (
    EXISTS (
      SELECT 1 FROM public.memberships m
      WHERE m.organization_id = projects.organization_id
        AND m.user_id = auth.uid()
    )
  );

Use WITH CHECK on insert/update mirroring the same rule.

Client auth

  • Use Supabase Auth session; gate routes with session check + redirect to login.
  • Read auth.uid() in policies; do not trust client-sent user_id without WITH CHECK.
  • For profile rows, create profile on signup (trigger or edge function), keyed to auth.users.id.

Performance

Wrap repeated subqueries in a security definer helper (e.g. is_org_member(org_id)) — see multi-tenant-backend. This avoids re-evaluating membership joins per row and prevents recursion when policies reference the same table they protect.

Use (select auth.uid()) inside policies (with the parentheses) so Postgres caches the value per statement instead of per row.

Avoid

  • Tables without RLS in production.
  • USING (true) on user data tables.
  • Storing roles only in localStorage.
  • Exposing service role in Vite VITE_* env vars.
  • Bypassing RLS with the service role from the client.

Verify before done

Ask the user to test with two accounts: user A must not read/write user B's rows. Flag as "needs manual check" if you cannot run two sessions.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.