Guides · Updated Nov 30, 2026 · By Lucy at KeySnare

Supabase roles: don't trust user_metadata, use app_metadata (or a roles table)

You asked your AI tool for an admin dashboard, and it stored role: 'admin' in the user's metadata, then checked user.user_metadata.role to show the admin page and wrote an RLS policy on the same field. It works in testing. The problem: every signed-in user can edit their own user_metadata, so anyone can make themselves admin with one line of JavaScript.

The short version. Supabase's docs say: "Users can edit their own user metadata, so a role or tenant ID stored there lets a user grant themselves access" and "Don't use user metadata, including the user_metadata claim in the JWT, in RLS policies or authorization logic" (User Management docs). Store roles in app_metadata, which users can't change and you set from a trusted server, or in a user_roles table added to the token with a custom access token hook. Remember a JWT only picks up changes after it refreshes.

Here's the pattern AI tools write all the time:

// sign-up form
await supabase.auth.signUp({ email, password, options: { data: { role: 'user' } } })

// admin page guard
if (user.user_metadata.role === 'admin') showAdminPanel()

-- RLS policy
create policy "Admins can read all orders" on public.orders for select
to authenticated
using ( (auth.jwt() -> 'user_metadata' ->> 'role') = 'admin' );

Anyone who signs up can open the browser console and run this with your public anon key:

await supabase.auth.updateUser({ data: { role: 'admin' } })
await supabase.auth.refreshSession()   // new token now says role: admin

That's a documented, normal call: updateUser with data updates the user's own metadata (JS reference). They could even skip it and send role: 'admin' in options.data at sign-up. Now the admin panel opens and the policy hands them every order.

Step 1: Find role checks in your code that read user_metadata

Run this from your project folder (or ask your AI tool to do the same search):

grep -rnE "user_metadata|userMetadata|raw_user_meta_data" \
  --include=*.ts --include=*.tsx --include=*.js --include=*.jsx --include=*.sql . \
  | grep -v node_modules \
  | grep -iE "role|admin|plan|tier|tenant|org|team|permission|credits"

Reading a display name or avatar from user_metadata is fine. Any line that decides what someone may do is a bug. Also look at sign-up triggers: a handle_new_user() function that copies new.raw_user_meta_data ->> 'role' into profiles.role lets people pick their own role at sign-up.

Step 2: Find RLS policies and functions that read user_metadata

Policies edited in the dashboard won't be in your repo, so check the live database in the SQL Editor:

-- policies
select schemaname, tablename, policyname, cmd, qual, with_check
from pg_policies
where qual ilike '%user_metadata%' or with_check ilike '%user_metadata%';

-- functions used by policies or triggers
select n.nspname as schema, p.proname as function
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public'
  and (p.prosrc ilike '%user_metadata%' or p.prosrc ilike '%raw_user_meta_data%');

Supabase's RLS docs are direct about it: a policy that relies on the user_metadata claim "can create security issues in your application as this information can be modified by authenticated end users" (auth.jwt() docs).

Step 3: Move roles to app_metadata from a trusted server

app_metadata lives in the raw_app_meta_data column, and users can't change it themselves. Set it from server code (an API route or Edge Function) with your secret key:

// SERVER ONLY. The secret / service_role key must never reach the browser.
import { createClient } from '@supabase/supabase-js'
const admin = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SERVICE_ROLE_KEY)

// only after you've checked the caller is allowed to grant roles
await admin.auth.admin.updateUserById(userId, { app_metadata: { role: 'admin' } })

Then remove the old field from code paths that grant access, and fix the sign-up trigger so it never copies a role from raw_user_meta_data. Give new users the default role on the server side instead.

Step 4: Read the role from app_metadata in RLS and on the server

drop policy "Admins can read all orders" on public.orders;

create policy "Admins can read all orders" on public.orders for select
to authenticated
using ( ((select auth.jwt()) -> 'app_metadata' ->> 'role') = 'admin' );

On the server, check app_metadata on the user returned by supabase.auth.getUser(), which asks the Auth server rather than trusting what the browser sent:

const { data: { user } } = await supabase.auth.getUser()
if (user?.app_metadata?.role !== 'admin') return new Response('Forbidden', { status: 403 })

Hiding the admin button in the UI is fine for looks, but the real check has to be in RLS or server code. If your roles live in a profiles.role or is_admin column instead, make sure users can't update that column on their own row (see RLS filters rows, not columns).

Step 5: Use a user_roles table and custom access token hook for real RBAC

For more than one simple role, Supabase's Custom Claims & RBAC guide uses a roles table plus an Auth hook that adds the role to every new token. The core of it:

create type public.app_role as enum ('admin', 'moderator');
create table public.user_roles (
  id      bigint generated by default as identity primary key,
  user_id uuid references auth.users on delete cascade not null,
  role    app_role not null,
  unique (user_id, role)
);
alter table public.user_roles enable row level security;
revoke all on table public.user_roles from authenticated, anon, public;

RLS must be on: without it, any signed-in user on a project with default grants could insert a row that makes themselves admin. Then create public.custom_access_token_hook(event jsonb) from that guide, which sets a user_role claim, grant it to supabase_auth_admin, enable it under Authentication → Hooks, and check permissions in policies through the guide's authorize() function:

create policy "Allow authorized delete access" on public.messages
for delete to authenticated
using ( (select authorize('messages.delete')) );

Step 6: Handle stale tokens and test the fix

Supabase warns that "a JWT is not always up-to-date": if you change app_metadata or a user's roles, auth.jwt() won't see it until that user's token refreshes. Promotions can wait for a refresh (the client can call supabase.auth.refreshSession()). For removing access to something sensitive, don't rely on the token alone; have the policy or server code check the roles table directly.

Then attack it yourself. Sign in as a normal test user and run:

await supabase.auth.updateUser({ data: { role: 'admin', plan: 'pro' } })
await supabase.auth.refreshSession()
// now open the admin page, call your admin API routes,
// and query the admin-only tables. Everything should still say no.

Re-run the Step 1 and Step 2 searches after every AI edit that touches auth or SQL, because a regenerated feature can bring the old pattern back.

What KeySnare checks. KeySnare doesn't connect to your Supabase project, so it can't see your policies, roles or user metadata. The free live-website check loads your page and the scripts it serves from your own domain and flags a Supabase service_role key (which bypasses RLS completely), Stripe secret keys and other secret keys anyone could copy. The free GitHub repo scan flags tables created without RLS in your SQL migrations, the service_role key in browser code and hardcoded keys in your current code (not Git history). Use the searches and tests above for roles.

Copy-paste prompt for your AI coding tool

Paste this into Cursor, Claude, Lovable, Bolt or whichever AI built your app:

Audit how my Supabase app decides who is an admin (or which plan, team or tenant a user belongs to). 1) Search the whole codebase, SQL migrations and database functions for user_metadata, userMetadata and raw_user_meta_data, and list every place one of them is used for a role, admin flag, plan, tier, tenant or permission check, including sign-up code and triggers that copy metadata into a profiles table. 2) List every RLS policy and function that reads auth.jwt() -> 'user_metadata' or raw_user_meta_data. 3) Move those values out of user_metadata: either into app_metadata, set only from server code with supabase.auth.admin.updateUserById(userId, { app_metadata: { role } }) using the secret/service_role key from a server environment variable, or into a public.user_roles table with RLS enabled and no grants for anon or authenticated, added to the JWT with a custom access token hook as in Supabase's Custom Claims & RBAC guide. 4) Rewrite the policies to use ((select auth.jwt()) -> 'app_metadata' ->> 'role') or the user_roles claim, and make server checks use user.app_metadata from supabase.auth.getUser(). 5) Make sure users cannot update a role, is_admin, plan or credits column on their own profile row. 6) Give me a test: sign in as a normal user, run supabase.auth.updateUser({ data: { role: 'admin' } }), refresh the session, and confirm no admin page, API route or row unlocks. Never put the service_role or secret key in browser code.

Check your live site for exposed keys in about a minute

Enter your app's URL on KeySnare for a free check of the page and its same-site JavaScript for a leaked service_role key and other secret keys, or paste a public GitHub repo for a free grade and your top 3 issues, each with a copy-paste AI fix prompt.

Run a free scan →

FAQ

Can a Supabase user change their own user_metadata?

Yes. Supabase's docs say any signed-in user can add, overwrite or remove fields in their own user_metadata, and Supabase Auth doesn't check the values. In JavaScript it is one call: supabase.auth.updateUser({ data: { role: 'admin' } }). Users can also set it at sign-up through options.data.

What is the difference between user_metadata and app_metadata?

user_metadata (stored in the raw_user_meta_data column of auth.users) is editable by the user and is meant for things like a display name or theme. app_metadata (stored in raw_app_meta_data) can't be changed by the user; you set it from a trusted server with your secret key, so it is the place for authorization data such as a role or plan.

How do I read app_metadata in an RLS policy?

Use auth.jwt(), for example ((select auth.jwt()) -> 'app_metadata' ->> 'role') = 'admin'. Wrapping auth.jwt() in a select lets Postgres evaluate it once per query. Never use the user_metadata claim in a policy.

Why didn't a role change take effect right away?

Because auth.jwt() reads the access token, and Supabase's docs warn a JWT is not always up to date: an app_metadata change shows up only after the user's token is refreshed. The client can call supabase.auth.refreshSession(). For removing access on sensitive actions, check a roles table directly instead of the token.

Should I use app_metadata or a user_roles table?

app_metadata is fine for a single simple role or plan. If you have several roles, permissions per role, or admins who manage roles inside the app, Supabase's Custom Claims & RBAC guide recommends a user_roles table plus a custom access token hook that copies the role into the JWT.

Does KeySnare check my Supabase roles and policies?

No. KeySnare doesn't connect to your database, so it can't see your policies, roles or user metadata. The free live-website check finds a Supabase service_role key and other secret keys in your site's JavaScript, and the GitHub repo scan flags tables created without RLS in SQL migrations and secrets in your current code (not Git history). Use the steps in this guide for roles.

Sources: Supabase User Management (Adding and retrieving user metadata) · Supabase Row Level Security (auth.jwt(), raw_user_meta_data vs raw_app_meta_data) · Supabase Custom Claims & RBAC (user_roles table, custom access token hook) · Supabase JS auth.admin.updateUserById · Supabase JS auth.updateUser

More guides: Supabase views bypass RLS: fix them with security_invoker · Supabase RLS filters rows, not columns · Is my Supabase app safe? How to check Row Level Security · How to hide API keys in Lovable, Bolt and Cursor apps