0
Challenge · 51Auth: Who Is Asking?

MFA, RBAC & custom claims

Authused in ~55% of apps

A junior stores a user's role as `{ role: 'admin' }` inside user_metadata, checks it in a policy, ships it — and any signed-in user can open dev tools and call `supabase.auth.updateUser({ data: { role: 'admin' } })` to promote themselves, because user_metadata is exactly as writable by the user as their own display name. Fixing that hole for good is only half of this concept. The other half is the lock most teams skip entirely: even a correctly-checked admin role should not be enough, on its own, to delete someone else's data — the highest-stakes actions should require proof of a second factor, checked at the exact moment the row is touched.

Core idea. A role that gates real actions must live somewhere the user cannot write to — a dedicated user_roles table, stamped onto every fresh JWT by a custom access token hook, so a policy can read auth.jwt() ->> 'user_role' for free instead of trusting a client-writable field or re-querying a table on every check. And the highest-privilege actions should additionally require a fresh second factor: the JWT's aal ("authenticator assurance level") claim, checked by a restrictive policy that can only subtract access, never grant it.
Playground, this isn't a screenshot
Where the role livesUser-writable?Safe for RLS?
user_metadataYes — updateUser({ data }) writes it directlyNever
app_metadataNo — server/Admin API onlyBetter, but couples RLS to a JSON blob nobody validates
user_roles table + access token hookNo — only the Auth server's hook can write the claimYes — a real table, a real source of truth, cheap to read from the JWT
↑ play a little first, then dig in