How to add admin roles to your web app
Add an admin role to your web app and check it on pages and actions, not just in the UI. The steps by hand, and what Launchfast already ships.
Short answer: add a role column to your users, write one guard that returns a 404 to anyone who isn't an admin, call it at the top of every admin page, and check the role again inside every action that changes data. Launchfast already does this: requireAdmin() in src/lib/admin.ts guards the page, Better Auth's admin plugin checks the role again on actions, and npm run make-admin -- you@example.com creates the first admin.
Why are roles easy to get wrong?
Adding a role column takes a minute. The mistakes come after it:
- You hide the admin link in the menu and call it done. Anyone who types
/adminstill gets in. - You check the role on the page but not in the action behind the "Ban" button. A signed-in user can call that action directly.
- You return a 403 "Forbidden" page, which tells a stranger that
/adminexists. - You can't create the first admin, because only an admin can make an admin.
- You trust a role sent from the browser, such as a hidden form field or a value in local storage.
How do you build roles by hand?
- Add the role to the user table. A
roletext column is enough to start. Leave it empty or set it touserfor everyone, and useadminfor the people you trust. If your auth library has an admin plugin, use it so the column and the checks match what the library expects. - Read the user on the server. Your guard must load the session from the cookie on the server. Never read the role from something the browser sends.
- Write one guard. A single function that gets the signed-in user, compares the role and stops the request. Return a 404 instead of a 403 so the page's existence isn't revealed. Send signed-out visitors to sign-in.
- Call the guard in every admin page. Don't rely on a shared layout or on middleware alone. Middleware that only checks for a session cookie saves a round trip, but it can't tell an admin from a customer.
- Check the role again in every action. Server actions and API routes are public endpoints. Each one that changes data must check the caller's role itself, even if the page that shows the button already did.
- Create the first admin. Sign up normally, then run a script or SQL statement that sets
role = 'admin'for your email, for exampleupdate "user" set role = 'admin' where email = 'you@example.com'. Do it from a terminal or the database console, never from a public route. - Protect admins from themselves. Hide the "ban" button on your own row so you can't lock yourself out, and decide what happens to a banned user's open sessions.
- Keep the page out of search. Set
noindexon admin pages and give them no page title. - Handle your framework's rendering rules. In Next.js 16 with
cacheComponentson, reading the session must happen inside a component wrapped in<Suspense>, or the build fails.
The traps are steps 4 and 5. A check in only one place, usually the page, is the most common way an admin area ends up open.
What does Launchfast already do?
Launchfast has all of this wired up. Here is what is in the repository.
The role and the plugin. src/lib/auth.ts registers Better Auth's admin() plugin, and src/db/schema.ts has the role, banned, banReason and banExpires columns on the user table.
The guard. requireAdmin() in src/lib/admin.ts:
export async function requireAdmin() {
const current = await requireUser();
if (current.role !== "admin") notFound();
return current;
}
requireUser() in src/lib/session.ts redirects signed-out visitors to /sign-in. A signed-in user who isn't an admin gets a 404. src/proxy.ts only checks that a session cookie exists for /admin, so it is a shortcut and not the security check. The page calls requireAdmin() itself.
The second check on actions. src/app/admin/actions.ts does not trust the page. It calls Better Auth's admin API with the request headers, and that API checks that the caller is an admin again:
export async function setBanned(formData: FormData) {
const userId = String(formData.get("userId"));
const ban = formData.get("ban") === "true";
const requestHeaders = await headers();
if (ban) {
await auth.api.banUser({ body: { userId, banReason: "Banned by an admin" }, headers: requestHeaders });
} else {
await auth.api.unbanUser({ body: { userId }, headers: requestHeaders });
}
refresh();
}
The comment on that function says Better Auth also signs a banned user out everywhere. In users-section.tsx the ban button is not shown on your own row.
The first admin. Sign up in the app, then run:
npm run make-admin -- you@example.com
scripts/make-admin.mts sets role = 'admin' on the account with that email. If no account exists for it, the script says so and exits without changing anything. npm run setup prints this command at the end as the next step.
Search and rendering. src/app/admin/page.tsx sets robots to noindex, nofollow and has no page title. The overview, payments table and user list are each inside <Suspense>, so the page builds with cacheComponents: true.
What should you know before relying on it?
make-admin connects with DATABASE_URL from .env.local, so it runs where you have that file, not in production. There is one admin role. If you need finer permissions such as "support can view but not ban", you will extend the admin plugin's roles yourself, which Launchfast doesn't do for you.
How much does this save?
You skip designing the role column, the guard, the second check on actions, the first-admin script and the self-lockout protection. What is left is signing up, running one command and opening /admin.
Launchfast is the starter this post describes. Related reading: how to build a SaaS admin dashboard with MRR, how to add Google and GitHub login to your web app and how to gate a feature behind a paid plan.