Admin checks in vibe-coded apps
A client-side isAdmin flag, a policy on user_metadata, and an API route with no session all trust the browser. Use app_metadata or a roles table, and reject a missing user.
Updated 27 October 2026 · LIM Agentic AI, Paris
An admin screen that returns different React when isAdmin is false is a view. The request that loads the rows is the control. If that request succeeds for any signed-in user, hiding the button did nothing. Generated apps tend to fail this in three places: a check that only runs in the client, a policy that reads user_metadata, and a server route that uses the secret key and never looks at a session.
The client check
const { data } = await supabase.auth.getUser();
if (data.user?.user_metadata?.role !== "admin") {
return <p>Admins only</p>;
}
return <AdminTable />;Anyone who can call from("billing").select() from the browser gets the rows the policies allow, whether or not this component rendered. Fix the policy. Then you can hide the button for ordinary users so the UI is less confusing. The button is not the fix.
user_metadata is editable by the user
user_metadata is the bag of fields a user is allowed to change. updateUser({ data }) writes it. There is no server-side allowlist.
await supabase.auth.updateUser({ data: { role: "admin" } });After the next token refresh, the JWT carries user_metadata.role = "admin". A policy that trusts that claim promotes whoever ran the call.
create policy "admins read billing"
on public.billing
for select
to authenticated
using (((auth.jwt() -> 'user_metadata') ->> 'role') = 'admin');app_metadata is the other bag. The user cannot write it with updateUser. You set it from server code that holds the secret key, through the Auth admin API. A policy may read it.
create policy "admins read billing"
on public.billing
for select
to authenticated
using (((auth.jwt() -> 'app_metadata') ->> 'role') = 'admin');await admin.auth.admin.updateUserById(userId, {
app_metadata: { role: "admin" },
});auth.jwt() reads the access token, not the user row. A change to app_metadata shows up in RLS after the token is refreshed, not on the next query. That delay is acceptable for a flag that rarely changes. It is a bad fit for a revoke that must work immediately. getUser() on the server talks to Auth and sees the current app_metadata even when the caller's JWT is stale. RLS using auth.jwt() does not. Do not mix them and expect them to agree in the minute after you demote someone.
A roles table
A table is checked on every query. Revoke is a row delete, and the next request sees it. Users can read their own role so the UI can hide the admin link. They cannot insert or update the table. The function that checks admin is security definer, so it can read the table even though the caller cannot read other people's rows. It takes no user id argument. It uses auth.uid() only. search_path is fixed so a stand-in table in another schema is not picked up.
create table public.user_roles (
user_id uuid primary key references auth.users (id) on delete cascade,
role text not null check (role in ('admin', 'member'))
);
alter table public.user_roles enable row level security;
create policy "read own role"
on public.user_roles
for select
to authenticated
using (auth.uid() = user_id);
revoke all on table public.user_roles from public, anon, authenticated;
grant select on table public.user_roles to authenticated;
create or replace function public.is_admin()
returns boolean
language sql
stable
security definer
set search_path = public
as $$
select exists (
select 1
from public.user_roles
where user_id = auth.uid()
and role = 'admin'
);
$$;
revoke all on function public.is_admin() from public, anon;
grant execute on function public.is_admin() to authenticated;Use it from policies with OR if an admin should see other people's rows. A normal user still has to match auth.uid() = user_id.
create policy "admins read notes"
on public.notes
for select
to authenticated
using (auth.uid() = user_id or public.is_admin());Grant no insert, update, or delete on user_roles to authenticated. Writes happen in the SQL editor or in a server route that uses the secret key after it has already decided the caller is allowed to appoint admins. A route that accepts { userId, role } from the body and writes the table with the secret key, without a session check, is the next bug.
Routes that answer with no session
This function is on the public internet. The secret key bypasses RLS. The body picks the user. Nothing in the handler looks at a JWT.
Deno.serve(async (req) => {
const { userId } = await req.json();
const admin = createClient(
Deno.env.get("SUPABASE_URL")!,
Deno.env.get("SUPABASE_SECRET_KEY")!,
);
const { data } = await admin.from("profiles").select("*").eq("id", userId);
return Response.json(data);
});Supabase Edge Functions can require a JWT at the gateway. That proves some signed-in user called the function. It does not prove they are an admin, and builders turn the gateway check off. Check the user inside the function anyway. getUser() validates the token with Auth. getSession() on the server only reads a token. Do not use getSession() as the access check.
Prefer a client created with the anon key and the caller's Authorization header. Queries then run as that user and RLS still applies. Use the secret key only for a step the user client is not allowed to do, and only after getUser() has returned a user you meant to allow.
export async function GET(request: Request) {
const url = process.env.SUPABASE_URL;
const anon = process.env.SUPABASE_ANON_KEY;
const header = request.headers.get("authorization");
if (!url || !anon || !header) return new Response("Unauthorized", { status: 401 });
const supabase = createClient(url, anon, {
global: { headers: { Authorization: header } },
});
const { data, error } = await supabase.auth.getUser();
if (error || !data.user) return new Response("Unauthorized", { status: 401 });
if (data.user.app_metadata?.role !== "admin") {
return new Response("Forbidden", { status: 403 });
}
const rows = await supabase.from("notes").select("id, user_id");
if (rows.error) return new Response("Bad request", { status: 400 });
return Response.json(rows.data);
}A 401 for a missing or bad token, and a 403 for a signed-in non-admin, tells you the check ran. A 200 and a row of data, with no Authorization header, means it did not.
Check it yourself
Search policies for user_metadata. Any row is a policy a user can satisfy by calling updateUser.
select n.nspname, c.relname, pol.polname,
pg_get_expr(pol.polqual, pol.polrelid) as using_expr,
pg_get_expr(pol.polwithcheck, pol.polrelid) as check_expr
from pg_policy pol
join pg_class c on c.oid = pol.polrelid
join pg_namespace n on n.oid = c.relnamespace
where n.nspname = 'public'
and (
pg_get_expr(pol.polqual, pol.polrelid) ilike '%user_metadata%'
or pg_get_expr(pol.polwithcheck, pol.polrelid) ilike '%user_metadata%'
);Search the app source for user_metadata, isAdmin, and service_role inside route handlers and edge functions. Then call the admin URL with no Authorization header. curl -i https://your-host/api/admin should be 401, not 200. Call it again with user B's access token. That should be 403, and it should not return user A's rows.
Sign in as a non-admin and run updateUser({ data: { role: "admin" } }). Reload. The UI may now show the admin link, because the UI trusts user_metadata. The API must still return 403, and pg_policies must show no policy reading user_metadata. If the API starts returning rows, the policy or the route is using the field the user just wrote.
Questions
Is app_metadata safe to put a role in?
Yes, as long as only the server, using the secret key, writes it. The user cannot set app_metadata through updateUser. Remember that auth.jwt() in a policy keeps the old value until the access token refreshes. For a revoke that has to work on the next request, use a roles table.
The edge function has Verify JWT turned on. Is that an admin check?
No. The gateway then rejects a missing user token. Any signed-in user still gets in. Call getUser() and then check app_metadata or the roles table before you read other people's rows.
Can the security definer function leak the whole roles table?
Not this one. It returns a boolean and it filters on auth.uid(). Do not add a user-id argument and trust it. Keep search_path set, and do not grant insert on user_roles to authenticated.