// guide · Supabase · updated Oct 22, 2026
Don't store roles in Supabase user_metadata: any logged-in user can make themselves admin
Short answer
Supabase's user_metadata is written by the user, not by you. One supabase.auth.updateUser() call from the browser changes it. If an RLS policy, a signup trigger or an API route decides who is an admin or a paying customer fromuser_metadata, any signed-in user can promote themselves. Keep roles in app_metadata (set only from the server) or in a roles table the client can't write to.
Why user_metadata can't be trusted
Supabase Auth keeps two JSON blobs on every user. user_metadata (raw_user_meta_data in auth.users) is meant for profile details the user supplies, and the user can update it at any time with their own session and your public anon key (auth.updateUser reference). app_metadata (raw_app_meta_data) can only be changed with theservice_role key or SQL. Both end up in the user's JWT, which is why they are easy to mix up. Supabase's own RLS docs warn that a policy relying on the user_metadata claim “can create security issues in your application as this information can be modified by authenticated end users” (Row Level Security).
This is all it takes for a normal user to flip their role:
// Run in the browser console while signed in as a normal user
await supabase.auth.updateUser({ data: { role: "admin", is_admin: true, plan: "pro" } });
await supabase.auth.refreshSession(); // new JWT now carries user_metadata.role = "admin"Where AI-built apps get this wrong
Lovable, Bolt, Cursor and Claude Code reach for user_metadata because it's the quickest place to stash a field at signup. Three patterns show up again and again:
-- Unsafe: trusts a value the user controls
create policy "admins can read everything" on public.orders
for select to authenticated
using ((auth.jwt() -> 'user_metadata' ->> 'role') = 'admin');
-- Unsafe: copies the signup's user-supplied data into a role column
create function public.handle_new_user() returns trigger as $$
begin
insert into public.profiles (id, role)
values (new.id, coalesce(new.raw_user_meta_data ->> 'role', 'user'));
return new;
end;
$$ language plpgsql security definer;// Unsafe: server route trusting user_metadata
const { data: { user } } = await supabase.auth.getUser();
if (user?.user_metadata?.role !== "admin") return new Response("Forbidden", { status: 403 });The same applies to plan, tier, is_pro, credits and similar flags: if they live in user_metadata, users can give themselves your paid plan for free. Reading these fields in browser code to show or hide buttons is fine only if the server and RLS enforce the real check.
The safe fix
Pick one trusted source for roles and read it everywhere. Supabase documents a roles table plus a custom access token hook for role-based access control (Custom claims and RBAC). A simpler version:
-- Option A: roles table the user cannot write to
create table public.user_roles (
user_id uuid primary key references auth.users on delete cascade,
role text not null default 'user'
);
alter table public.user_roles enable row level security;
create policy "users can read their own role" on public.user_roles
for select to authenticated using (auth.uid() = user_id);
-- no insert/update/delete policies: only service_role or SQL can change roles
create policy "admins can read everything" on public.orders
for select to authenticated
using (exists (select 1 from public.user_roles r
where r.user_id = auth.uid() and r.role = 'admin'));
-- Option B: app_metadata, set only from a trusted server
-- supabase.auth.admin.updateUserById(id, { app_metadata: { role: "admin" } }) // service_role key, server only
-- policy: using ((auth.jwt() -> 'app_metadata' ->> 'role') = 'admin')
-- Signup trigger: copy only harmless profile fields
insert into public.profiles (id, full_name)
values (new.id, new.raw_user_meta_data ->> 'full_name');After changing a policy, existing sessions keep their old JWT until it refreshes, so test with a fresh sign-in. More on the difference between the two metadata fields is in Managing user data.
Test it in two minutes
- Sign in as a normal test account.
- Run the
updateUsersnippet above in the browser console, then reload. - Try the admin page, a paid feature, and an API route that should be admin-only. Every one should still say no.
Tell your AI coding tool
Paste this into Lovable, Bolt, Cursor or Claude Code to find and fix it across the project:
Audit this project for authorization that trusts Supabase user_metadata (raw_user_meta_data).
1. Find every place that reads role, is_admin, admin, plan, tier, subscription, credits or permissions from user_metadata: RLS policies (auth.jwt() -> 'user_metadata'), triggers such as handle_new_user, server routes and edge functions (user.user_metadata.*), and browser code.
2. Move those values to a user_roles table with RLS (users may only select their own row, no client writes) or to app_metadata set from the server with the service_role key.
3. Update every policy and server check to read the trusted source. Keep user_metadata only for display fields like full_name and avatar_url.
4. Add a test: a normal user calls supabase.auth.updateUser({ data: { role: "admin" } }) and must still be denied.Check your repo automatically
RescueMyRepo's free scan flags roles, admin flags and plans read from user_metadata in signup triggers, RLS policies, server routes and browser code, alongside tables without RLS, permissive policies, public storage buckets and leaked keys, with a fix prompt for your AI tool.
FAQ
Can a user change their own Supabase user_metadata?
Yes. Any signed-in user can call supabase.auth.updateUser({ data: { ... } }) with the public anon key and their own session, and whatever they send is saved to user_metadata (raw_user_meta_data in auth.users). Supabase's RLS docs warn that policies relying on user_metadata can create security issues because end users can modify it.
Is app_metadata safe for roles?
app_metadata (raw_app_meta_data) cannot be changed by the user from the client; it is set from a trusted server with the service_role key or from SQL. It is a reasonable place for a role or plan claim. A separate roles table protected by RLS, read through a custom access token hook, is Supabase's documented pattern for role-based access control.
Is it OK to read user_metadata in a signup trigger?
Only for harmless profile fields such as full_name or avatar_url. The signup options.data object is user-controlled, so a handle_new_user trigger that copies raw_user_meta_data->>'role' into a profiles table lets anyone sign up as admin.
How do I test whether my app is vulnerable?
Sign in as a normal test user, run supabase.auth.updateUser({ data: { role: 'admin', is_admin: true, plan: 'pro' } }) in the browser console, refresh the session, and see whether any admin page, paid feature or other user's data opens up. If anything changes, your authorization reads user_metadata.
Does hiding the admin button fix it?
No. The check has to happen where the data is: in RLS policies or server code that reads a trusted source. Anything in browser code can be skipped by calling the API directly.