Supabase Backend as a Service · Lezione

Accesso basato sui ruoli con RLS e custom claims

Costruisca policy avanzate di Row-Level Security che concedano accessi diversi in base al ruolo dell’utente, usando i claims JWT e funzioni di supporto.

Lezione 3 di 313 passaggi

Accesso basato sui ruoli con RLS e custom claims è una lezione Supabase Backend as a Service gratuita su CoddyKit. Questa è la lezione 3 di 3. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Supabase Backend as a Service, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Supabase Backend as a Service include 3 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Beyond Owner-Only Policies

Basic RLS often checks auth.uid() for ownership. Real apps also need role-based rules, like admins seeing everything and editors seeing more than viewers.

Where Roles Live

You can store roles in a column on a profiles table, or embed them as custom claims in the user's JWT for fast, joinless checks.

Reading the JWT in SQL

Supabase exposes the JWT via auth.jwt(). You can read custom claims from it.

-- returns the 'user_role' claim as text
select auth.jwt() ->> 'user_role';

A Role Helper Function

Wrap the claim read in a helper so policies stay readable.

create or replace function current_role_name()
returns text
language sql stable
as 'select coalesce(auth.jwt() ->> ''user_role'', ''viewer'')';

Admin-Sees-All Policy

Grant admins unrestricted read access while others are limited.

create policy admin_read on documents
for select
using (current_role_name() = 'admin');

Combining Ownership and Role

Policies can OR conditions: a user sees a row if they own it OR they are an admin.

create policy read_own_or_admin on documents
for select
using (
  owner_id = auth.uid()
  or current_role_name() = 'admin'
);

Separate Policies per Action

Define distinct policies for SELECT, INSERT, UPDATE, DELETE so each action has the right rule, for example editors can update but viewers cannot.

Modeling a Role Hierarchy

Map roles to permission levels so you can compare numerically.

function level(role) {
  return { viewer: 1, editor: 2, admin: 3 }[role] || 0;
}
console.log(level('editor') >= level('viewer'));

Setting Custom Claims

Custom claims are added to the JWT via an auth hook or by a trusted server using the admin API. Never let clients set their own role.

Security Reminders

Always keep RLS enabled on the table. Helper functions should be stable, and role logic must never trust client-supplied values directly.

Putting It Together

Combine ownership checks with role claims read from the JWT to express rich, secure access rules entirely in the database.

Quick Check

Test your understanding of role-based RLS.

Recap

You built role-based RLS using auth.jwt() custom claims, helper functions, combined ownership-or-role policies, per-action rules, and learned to set roles only on a trusted server.

Gratis per iniziare

Impara Supabase Backend as a Service con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
11
Lezioni
40

Domande Frequenti

La lezione «Accesso basato sui ruoli con RLS e custom claims» è gratuita?

Sì — il testo completo di «Accesso basato sui ruoli con RLS e custom claims» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Supabase Backend as a Service, passa a CoddyKit PRO. Il corso Supabase Backend as a Service include 3 lezioni in totale.

Cosa imparerò in «Accesso basato sui ruoli con RLS e custom claims»?

Costruisca policy avanzate di Row-Level Security che concedano accessi diversi in base al ruolo dell’utente, usando i claims JWT e funzioni di supporto. Eserciti Supabase Backend as a Service con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Supabase Backend as a Service?

Non è richiesta alcuna esperienza precedente. Supabase Backend as a Service su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 3.

Quanto tempo richiede la lezione «Accesso basato sui ruoli con RLS e custom claims»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Supabase Backend as a Service?

Sì. Ogni lezione Supabase Backend as a Service include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Introduzione alle policy RLS
  2. Test e debug di RLS
  3. Accesso basato sui ruoli con RLS e custom claims
← Torna a Supabase Backend as a Service