Guides

How to add user roles — admin and regular, with no hole in access

Illustration: two doors, one with a badge reader, a single guard post behind both

Ask an AI to "add an admin role" and nine times out of ten you get this:

{user.isAdmin && <button onClick={deleteAllUsers}>Delete all</button>}

It looks like it works: a regular user doesn't see the button. But a button isn't protection. Open the browser console, fire the same request by hand, and it goes through — because the server never asked anything. A role that lives only in the interface isn't a role, it's a polite request.

Below are the five steps that make roles real. The order matters: the interface comes last, not first.

1. Decide where the role lives

A role is data about a user, so it belongs in the database, next to the profile. Not in localStorage, not in app state, not in a hidden form field — all of that sits on the user's side, which means the user can edit it.

One column with a safe default is the whole thing:

alter table profiles
  add column role text not null default 'user';

Don't start with a roles table, a permissions table and a join between them. For 99% of first projects two values are enough: user and admin. Extending later is ten minutes of work; untangling your own permissions system at the start is a week of it. If you're adding the column to a live project, ship it as a migration rather than by hand in a dashboard.

2. Grant the first admin manually

Chicken and egg: there's no UI for granting roles yet, but you already need an admin. Appoint the first one directly in the database:

update profiles set role = 'admin'
where email = '[email protected]';

Never build a public "make me admin" endpoint — not even temporarily, not even "for five minutes to test." That trap and its neighbours are covered in the pre-launch security checklist.

3. Lock the data down on the server — this is the step that matters

The check has to live where the user can't reach it: in the database or in a server-side handler. This is exactly the difference between authentication and authorization: login already established who this is, now we decide what they may do.

If you're on Postgres with RLS (Supabase, for instance), the rule goes into the database itself and can't be bypassed even with a valid token:

create policy "admins delete any profile"
on profiles for delete
using (
  (select role from profiles where id = auth.uid()) = 'admin'
);

If you have your own backend, the same thought goes in the handler, as its very first line:

const me = await db.profiles.findById(session.userId);
if (me.role !== 'admin') {
  return res.status(403).json({ error: 'forbidden' });
}

Two principles at work here:

  • The role is read from the database, not from the request. If the client sends {"role":"admin"}, that's not data, that's a wish. Ignore it.
  • The status code is 403, not 401. 401 means "I don't know who you are," 403 means "I do, and no." Why they aren't the same is its own breakdown, and confusing them breaks your client logic.

4. Hide the interface — but only as a convenience

Now you can go back to that button. You should hide it, but for a different reason: so nobody clicks something that will be refused anyway. That's user experience, not security.

{me.role === 'admin' && <AdminPanel />}

Keep the formula in mind: the server forbids, the interface doesn't tempt. Drop step 4 and your app gets awkward. Drop step 3 and it gets leaky.

5. Attack yourself

Five minutes of work that catches almost every hole. Log in as a regular user, copy your token from devtools, and knock on the admin endpoint directly:

curl -X DELETE https://your-site/api/users/7 \
  -H "Authorization: Bearer REGULAR_USER_TOKEN"

You want a 403. If you get 200, step 3 didn't take — and it's better to learn that now than from the logs after an incident.

What you end up with: the admin sees and does more, the regular user doesn't — and that's true at the data level, not just in the UI. A role changes with one line in the database, no release needed. Best of all, when someone asks "what if they call the API directly?" you have a tested answer.

How many roles should I start with?

Two: user and admin. Add a third when a real scenario refuses to work without it — not "for the future." A flexible permissions system nobody uses is the most expensive kind of unused code.

Store the role in the JWT or read it from the database each time?

A role in the token saves you a database query, but tokens have a lifetime: demote an admin and their token still acts admin for another hour. For a first project, read from the database. If you do put it in the token, keep the lifetime short and know how to revoke a session.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →