Guides · Updated 2026-11-08
How to store admin roles safely in Supabase (user_metadata vs app_metadata vs a roles table)
Short answer: Never store admin or role flags in user_metadata. A signed-in user can change their own user_metadata through the client SDK (updateUser, passing a data object), and any Row Level Security policy that trusts auth.jwt() -> 'user_metadata' lets them make themselves an admin. app_metadata cannot be updated by the user — only a trusted server using the Auth Admin API (for example updateUserById with an app_metadata object) can set it. A safer pattern is a user_roles (or roles) table that ordinary users cannot write to, optionally paired with a Custom Access Token Hook that copies the role into the JWT as a custom claim. Then confirm with a second test account that cannot promote itself or read admin-only rows.
Why "isAdmin" in user_metadata is so common — and unsafe
When you ask an AI coding tool to "add admin users" or "only admins can see the dashboard", it often hangs a flag on the user object: role: "admin", isAdmin: true, or similar. The convenient place is user_metadata, because it already shows up after sign-up and on supabase.auth.getUser(). Supabase's user-management docs show that the data object you pass to signUp (options.data) is stored on auth.users as raw_user_meta_data and read back as user.user_metadata (https://supabase.com/docs/guides/auth/managing-user-data).
That field is meant for things the user themselves should be able to change — a display name, an avatar, a theme. It is not meant for authorization. Supabase's Users page says it directly: do not use user_metadata in a security-sensitive context such as RLS policies or authorization logic, because the value is editable by the user without any checks (https://supabase.com/docs/guides/auth/users).
If your database policy or your app code treats "user_metadata.role === admin" as proof that someone is an admin, any signed-in person can promote themselves. The public anon / publishable key in your page does not stop this — that key is supposed to be public. What is supposed to stop it is a rule the user cannot rewrite. More on that key: https://leakyvibe.com/guides/is-supabase-anon-key-safe-to-expose
user_metadata vs app_metadata
Both fields travel in the user's JWT (the signed session token). Inside Postgres you can read that token with auth.jwt(). Supabase's Row Level Security guide spells out the difference (https://supabase.com/docs/guides/database/postgres/row-level-security):
- raw_user_meta_data (user_metadata in the JWT and in the user object) can be updated by the authenticated user. The RLS guide names this supabase.auth.update(); the current JavaScript client method is updateUser. It is not a good place to store authorization data.
- raw_app_meta_data (app_metadata in the JWT) cannot be updated by the user, so it is a good place to store authorization data.
The same RLS page warns that a policy which relies on the user_metadata claim can create security issues, because authenticated end users can modify that information.
What a signed-in user can run from the browser, using the official client (https://supabase.com/docs/reference/javascript/auth-updateuser). The data field updates user_metadata — not app_metadata:
// Anyone who is signed in can change their own user_metadata this way.
const { data, error } = await supabase.auth.updateUser({
data: { role: 'admin', isAdmin: true }
})app_metadata is different. The documented way to set it is the Auth Admin API on a server, with your project's secret / service_role key — never in the browser (https://supabase.com/docs/reference/javascript/auth-admin-updateuserbyid):
// Server only. Never expose the service_role / secret key in the browser.
const { data: user, error } = await supabase.auth.admin.updateUserById(
userId,
{ app_metadata: { plan: 'trial' } }
)The Users page describes app_metadata as holding the sign-in provider list (provider and providers). You can also attach your own keys there — the Admin API example above uses plan: "trial", and the Custom Access Token Hook docs set an admin flag under app_metadata. A policy can read those keys, for example the official team check: team_id in (select auth.jwt() -> 'app_metadata' -> 'teams').
One caveat, also from the RLS guide: a JWT is not always up to date. If you remove someone from a team or take away admin in app_metadata, auth.jwt() will keep showing the old value until that user's token is refreshed. For a change that must take effect immediately (firing an admin, locking an account), do not rely on the token alone.
A safer pattern: a roles table, and optionally a Custom Access Token Hook
Supabase's recommended way to do role-based access is a table you control, plus an Auth Hook that copies the role into the JWT as a custom claim (https://supabase.com/docs/guides/api/custom-claims-and-role-based-access-control-rbac). The table is the source of truth; the claim is a convenient copy for policies to read.
Create a user_roles table that references auth.users. The official guide uses an app_role enum (admin, moderator) and a separate role_permissions table if you want finer permissions. A minimal shape looks like this:
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 text not null, unique (user_id, role) ); alter table public.user_roles enable row level security; revoke all on table public.user_roles from anon, authenticated;
That revoke is the important part. If signed-in users can insert or update their own row, the table is no safer than user_metadata. Assign roles only from a trusted server path that uses the secret / service_role key (and never ships that key to the browser). Do not put an is_admin column on a regular profiles table that users are allowed to update — a common AI-generated policy is "users can update their own profile", which would let them flip the flag. The Custom Access Token Hook docs show an is_admin column on profiles only after granting that table to supabase_auth_admin and revoking it from authenticated, anon, and public (https://supabase.com/docs/guides/auth/auth-hooks/custom-access-token-hook). A separate user_roles table is clearer.
You can check the table from a policy (often through a small security-definer helper, which the RLS guide describes). Or you can inject the role into each new access token with a Custom Access Token Hook, which runs before Supabase Auth issues the token. The official RBAC guide reads user_roles and sets a user_role claim — it does not overwrite the top-level role claim, which must stay the Postgres role (anon or authenticated):
-- Adapted from Supabase's Custom Claims & RBAC guide.
create or replace function public.custom_access_token_hook(event jsonb)
returns jsonb
language plpgsql
stable
as $$
declare
claims jsonb;
user_role text;
begin
select role into user_role
from public.user_roles
where user_id = (event->>'user_id')::uuid;
claims := event->'claims';
if user_role is not null then
claims := jsonb_set(claims, '{user_role}', to_jsonb(user_role));
else
claims := jsonb_set(claims, '{user_role}', 'null');
end if;
event := jsonb_set(event, '{claims}', claims);
return event;
end;
$$;
grant usage on schema public to supabase_auth_admin;
grant execute on function public.custom_access_token_hook to supabase_auth_admin;
revoke execute on function public.custom_access_token_hook from authenticated, anon, public;
grant all on table public.user_roles to supabase_auth_admin;
revoke all on table public.user_roles from authenticated, anon, public;In the dashboard, the same guide says to open Authentication > Hooks (Beta) and select that Postgres function. After the hook runs, a policy can read the claim with auth.jwt() ->> 'user_role' (the official authorize() helper does exactly that). A simple version of the same check is (select auth.jwt() ->> 'user_role') = 'admin'. The hook page's "Add admin role" example instead sets app_metadata.admin to true and reads it from there — either custom claim is fine, as long as it did not come from user_metadata.
Two details that surprise people: the hook changes the access-token JWT, not the rest of the auth response, so to read the claim in the browser you decode session.access_token (the RBAC guide shows jwt-decode for this). And the claim still goes stale until the next refresh — the table (or a helper that reads the table) is what you want when a demotion must stick immediately.
How to check with a second test account
You do not need to attack anyone else's project. Do this on your own app, with two accounts you control. Call them A (an admin, or whoever should see admin-only data) and B (a normal user who must not).
- Sign up account B in a private browser window so you are not mixed up with A's session.
- While signed in as B, look at what your app uses as the admin flag (role, isAdmin, admin, and similar keys on the user object or in your policies).
- As B, run updateUser with a data object that sets those same keys to an admin value — the official call is supabase.auth.updateUser({ data: { ... } }). If your app has a "edit profile" screen that writes user_metadata, you can also try that. Then refresh the page, or sign out and back in so a new token is issued.
- Still as B, try to open admin pages, list other users, or read rows that should be admin-only (an orders table, a users table, anything A can see that B should not).
- Separately, as B, try to insert a row into user_roles or flip an is_admin column on profiles. If that write succeeds, the table itself is user-writable.
- Also try as B without changing any metadata: can B read A's admin-only rows just by being signed in? That usually means a policy only checks "the caller is logged in", not a real role. The same two-account idea is how you check whether a table is public: https://leakyvibe.com/guides/how-to-check-if-supabase-table-is-public
If B can become an admin, or can read or change admin-only rows, your rules are trusting something the user can edit (often user_metadata) or they are not checking a role at all. Hiding an /admin link in the UI is not enough — leftover admin pages and APIs need a real server-side check too (https://leakyvibe.com/guides/leftover-debug-admin-seed-routes-vibe-coded-apps).
How to lock it down
- Search your code, SQL migrations, and RLS policies for user_metadata.role, user_metadata.isAdmin, raw_user_meta_data, and auth.jwt() -> 'user_metadata'. Treat every hit as a bug until you confirm it is only used for display (name, avatar, preferences).
- Move the source of truth to a user_roles table that anon and authenticated cannot write, and/or to app_metadata set only through the Auth Admin API on a server.
- Update every policy and every server-side admin check to read the table, auth.jwt() -> 'app_metadata', or a custom claim such as user_role. Never read user_metadata for authorization.
- If you use a Custom Access Token Hook, grant it to supabase_auth_admin, revoke it from authenticated, anon, and public, and do not overwrite the top-level JWT role claim (keep that as anon or authenticated).
- Keep frontend checks (hiding a menu item, redirecting /admin) as user experience only. The database policy is what actually protects rows.
- After you demote someone, remember their existing access token can keep the old claim until it expires or refreshes. For a change that must stick immediately, have the sensitive policy read the roles table (or a helper that does), not only the JWT.
Prompt to give your AI coding tool
Audit how my app stores and checks admin roles in Supabase. Search the codebase, RLS policies, and SQL migrations for user_metadata.role, user_metadata.isAdmin, raw_user_meta_data, and any auth.jwt() -> 'user_metadata' checks. Those values are editable by the signed-in user via supabase.auth.updateUser({ data }) and must not gate admin access.
Then:
1) Create a public.user_roles table (user_id uuid references auth.users on delete cascade, role text or an enum, unique (user_id, role)). Enable RLS. Revoke ALL on that table from anon and authenticated so signed-in users cannot insert or update their own role. Only a trusted server path using the secret/service_role key (never exposed in the browser) should assign roles.
2) Optional: add a Custom Access Token Hook (Postgres function granted to supabase_auth_admin; revoke execute from authenticated, anon, and public) that reads user_roles and sets a custom claim such as user_role. Do not overwrite the top-level JWT role claim — that must stay anon or authenticated.
3) Update every RLS policy and every server-side admin check to use the roles table and/or auth.jwt() -> 'app_metadata' / the custom user_role claim — never user_metadata. Frontend hiding of admin UI is UX only.
4) If an is_admin flag currently lives on a user-writable profiles table, move it off that table.
5) Do not print any secret key values. Summarize the files and policies you changed, and how a second test account is blocked from promoting itself or reading admin-only rows.Quick checklist
- No admin, role, or isAdmin flag is stored in user_metadata, and no policy reads auth.jwt() -> 'user_metadata' for authorization.
- Roles live in a user_roles (or similar) table that signed-in users cannot write, and/or in app_metadata set only by the Auth Admin API on a server.
- RLS policies and server checks read the table, app_metadata, or a hook-injected custom claim (for example user_role) — never user_metadata.
- A second test account cannot promote itself with updateUser({ data }) or by writing to the roles table, and cannot read admin-only rows.
- Demoting an admin is reflected immediately for sensitive actions (table lookup), not only after the JWT refreshes.
The free LeakyVibe scan at the top of this page checks your live site from the outside for secret keys in public JavaScript, Supabase tables that answer the public key, and missing security headers. It does not check whether your policies trust user_metadata or whether a second account can make itself admin — still useful as a first pass, then use the second-account test and checklist above.
Related: how to check if a Supabase table is public — https://leakyvibe.com/guides/how-to-check-if-supabase-table-is-public — and whether the anon key is safe to expose: https://leakyvibe.com/guides/is-supabase-anon-key-safe-to-expose
Official sources (checked 2026-11-08): Supabase, "User Management" (https://supabase.com/docs/guides/auth/managing-user-data); Supabase, "Users" — do not use user_metadata in RLS or authorization, it is editable by the user (https://supabase.com/docs/guides/auth/users); Supabase, "Row Level Security" — raw_user_meta_data vs raw_app_meta_data, auth.jwt(), JWT staleness (https://supabase.com/docs/guides/database/postgres/row-level-security); Supabase JavaScript, updateUser (https://supabase.com/docs/reference/javascript/auth-updateuser); Supabase JavaScript, updateUserById (https://supabase.com/docs/reference/javascript/auth-admin-updateuserbyid); Supabase, "Custom Claims & Role-based Access Control (RBAC)" (https://supabase.com/docs/guides/api/custom-claims-and-role-based-access-control-rbac); Supabase, "Custom Access Token Hook" (https://supabase.com/docs/guides/auth/auth-hooks/custom-access-token-hook).