Short answer: never decide who is an admin from user_metadata (raw_user_meta_data) or from a role column that users can update on their own profiles row. Any signed-in user can change their own user_metadata by calling supabase.auth.updateUser({ data: { role: "admin" } }) from the browser. Store roles in app_metadata, which only the service role can change, or in a separate user_roles table that users can read but not write. Then check the role on the server and in RLS policies, not just in the UI.
This is one of the most common privilege escalation bugs in apps built with Lovable, Bolt, v0, Cursor and Replit. The admin dashboard is hidden behind if (user.role === "admin") in React, the role lives somewhere the user can write, and anyone who reads the docs can promote themselves.
The two vulnerable patterns
Pattern 1: role in user_metadata
AI tools often save extra signup fields into options.data, and sometimes the role goes there too:
// VULNERABLE: user_metadata is user-editable
await supabase.auth.signUp({
email,
password,
options: { data: { full_name: name, role: "user" } },
});
// Later, in the app
const isAdmin = user.user_metadata.role === "admin";
The problem is that the user can run this in the browser console at any time, using the public anon key that's already in your bundle:
await supabase.auth.updateUser({ data: { role: "admin" } });
The same applies to RLS policies that read auth.jwt() -> 'user_metadata'. Supabase's own docs warn against using user_metadata for authorization for exactly this reason.
Pattern 2: role column on profiles with a loose update policy
The other common setup is a profiles table with a role or is_admin column, plus a policy like this:
-- Lets users update their own row, including the role column
create policy "Users update own profile"
on public.profiles for update
to authenticated
using ( (select auth.uid()) = id );
RLS controls which rows a user can change, not which columns. So this policy lets a user run:
await supabase.from("profiles").update({ role: "admin" }).eq("id", user.id);
If your admin checks read profiles.role, they're now an admin.
Fix option 1: store the role in app_metadata
app_metadata (raw_app_meta_data) is included in the user's JWT and can't be changed by the user. Only code holding the service role key can set it, so do it from a trusted server, a script, or an Edge Function:
// Server only. Never ship the service role key to the browser.
import { createClient } from "@supabase/supabase-js";
const admin = createClient(process.env.SUPABASE_URL!, process.env.SUPABASE_SERVICE_ROLE_KEY!);
await admin.auth.admin.updateUserById(userId, {
app_metadata: { role: "admin" },
});
Then use it in RLS:
create policy "Admins can read all orders"
on public.orders for select
to authenticated
using ( (auth.jwt() -> 'app_metadata' ->> 'role') = 'admin' );
One caveat: the role is read from the JWT, so a change only takes effect after the user's token refreshes (by default within about an hour, or on their next sign in). When you remove someone's admin role, it can linger until then. If you need instant revocation, use option 2.
Fix option 2: a separate roles table
A dedicated table gives you instant changes and room for multiple roles:
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', 'moderator'))
);
alter table public.user_roles enable row level security;
-- Users can see their own role. No insert, update or delete policies,
-- so only the service role (or SQL editor) can change roles.
create policy "Users read own role"
on public.user_roles for select
to authenticated
using ( (select auth.uid()) = user_id );
Wrap the check in a helper function so policies stay readable:
create or replace function public.is_admin()
returns boolean
language sql
stable
security definer
set search_path = ''
as $$
select exists (
select 1 from public.user_roles
where user_id = (select auth.uid()) and role = 'admin'
);
$$;
create policy "Admins read all orders"
on public.orders for select
to authenticated
using ( (select public.is_admin()) );
security definer lets the function read user_roles regardless of the caller's policies, and the empty search_path prevents schema hijacking. If you write other functions like this, read Supabase RPC and security definer functions first, because a careless one can bypass RLS for everyone.
Supabase also supports a Custom Access Token Auth Hook that copies a role from a table like this into the JWT. That's a good choice for larger apps, but it has the same token refresh delay as option 1.
If you keep a role column on profiles, lock the column
Sometimes it's simpler to keep profiles.role. In that case, stop users from writing that column. Postgres column privileges do this:
-- Remove blanket update rights, then grant only safe columns
revoke update on public.profiles from authenticated;
grant update (full_name, avatar_url, bio) on public.profiles to authenticated;
Now an update that touches role fails with a permission error, even though the RLS policy allows the row. Remember to update the grant when you add new user-editable columns, and check inserts too: if users can insert their own profile row, they can insert it with role = 'admin'. Create profiles with a trigger on auth.users that sets the role server side instead.
Check roles on the server, not just in the UI
Hiding the admin link in React is a UI nicety, not security. Every admin API route, server action and Edge Function must check the role itself:
// app/api/admin/users/route.ts
import { createClient } from "@/lib/supabase/server";
export async function GET() {
const supabase = await createClient();
const { data: { user } } = await supabase.auth.getUser();
if (!user) return Response.json({ error: "Unauthorized" }, { status: 401 });
const { data: isAdmin } = await supabase.rpc("is_admin");
if (!isAdmin) return Response.json({ error: "Not found" }, { status: 404 });
// ...admin-only work
}
Use getUser(), which verifies the token with Supabase Auth, rather than getSession(), which trusts the cookie as-is. See getSession vs getUser. And don't rely on Next.js middleware alone for the admin area; older versions could be skipped entirely, as covered in the middleware bypass CVE-2025-29927 guide.
How to test it
Sign in as a normal user, open the browser console, and try to promote yourself:
- Run
supabase.auth.updateUser({ data: { role: "admin", is_admin: true } }), refresh, and see whether any admin features appear. - Run
supabase.from("profiles").update({ role: "admin" }).eq("id", "<your id>")and check whether it succeeds. - Call your admin API routes directly with a normal user's cookie.
All three should fail. Then search your code and SQL for user_metadata, raw_user_meta_data, is_admin and role to find every place an authorization decision is made, and make sure each reads from a source users can't write.
Related guides
Get an automated check
PreflightPR reviews AI-built apps for roles stored in user-editable places, missing Supabase RLS, unprotected admin routes, leaked keys and other launch blockers. Run a free scan on a public repo for a launch score and a prioritized fix list, or install the PreflightPR GitHub App to check every pull request before it merges.