Auth Checked Only in the UI: Hiding a Button Does Not Protect the Endpoint
Anything that runs in the browser can be skipped. A hidden "Delete" button, a route guard that redirects to /login, an if (user.role === 'admin') around a menu item: all of these change what the user sees, and none of them stop a request sent straight to your API. Every server endpoint has to check who is calling and whether they are allowed, on its own, every time.
On this page
This is the most common access problem in AI-built apps we look at. You ask an assistant to "make the admin page visible only to admins". It does exactly that. The page is hidden. The endpoint behind it answers anyone.
What it looks like
A React component decides what to show:
{user?.role === 'admin' && (
<button onClick={() => fetch(`/api/users/${id}`, { method: 'DELETE' })}>
Delete user
</button>
)}
And the handler does the work without asking questions:
// app/api/users/[id]/route.ts
export async function DELETE(req: Request, { params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
await db.user.delete({ where: { id } });
return Response.json({ ok: true });
}
A normal user never sees the button. They do not need it:
curl -X DELETE https://yourapp.com/api/users/42
Variations of the same mistake:
- A
ProtectedRoutecomponent oruseEffectredirect is the only login check. The page's data API has none. - The role is read from
localStorageor from a field the client can change. - The server checks that someone is logged in, but not that they are an admin.
- The admin area lives at a URL nobody links to.
Find it in your app
From the outside. Open the app as an admin with the browser's network tab open. Click through every admin action and copy each request as cURL. Then replay them twice: with no cookies or token, and with the session of a normal user. Anything that returns 200 is a finding.
From the code. List every route handler, server action and RPC function. For each, find the line that identifies the caller. This command gives a quick first pass for a Next.js project:
grep -rL "getSession\|auth()\|getUser" app/api --include=route.ts
It lists route files that never mention your auth helper. Adjust the pattern to whatever your project uses. Some results will be legitimately public (a health check, a webhook with signature verification). Each one should be a decision, not an accident.
Fix it on the server
Check identity and permission inside the handler, before any work:
import { auth } from '@/lib/auth';
export async function DELETE(req: Request, { params }: { params: Promise<{ id: string }> }) {
const session = await auth();
if (!session) {
return Response.json({ error: 'Unauthorized' }, { status: 401 });
}
if (session.user.role !== 'admin') {
return Response.json({ error: 'Forbidden' }, { status: 403 });
}
const { id } = await params;
await db.user.delete({ where: { id } });
return Response.json({ ok: true });
}
The role must come from the server's own record: the session store, the database, or a signed token the server issued. Never from the request body or a header the client sets.
To avoid repeating this in forty files and forgetting the forty-first, wrap it:
export async function requireAdmin() {
const session = await auth();
if (!session) throw new HttpError(401);
if (session.user.role !== 'admin') throw new HttpError(403);
return session.user;
}
Then every admin handler starts with const admin = await requireAdmin();. A handler without that line stands out in review.
Keep the UI checks too
Hiding controls a user cannot use is good design. Keep it. Just treat it as presentation. The rule that makes this easy to remember: the UI decides what to show, the server decides what to allow.
Middleware is not enough on its own
Putting one check in middleware and calling it done is tempting. Middleware only runs for the paths it matches, and the default examples often leave /api out. We wrote a separate post on middleware that skips API routes.
If your app talks to Supabase or Firebase directly from the browser, there is no handler to put the check in. The database rules are your server-side check: see Supabase RLS mistakes.
Add a test that fails when it regresses
test('normal user cannot delete users', async () => {
const res = await fetch(`${BASE}/api/users/42`, {
method: 'DELETE',
headers: { cookie: normalUserCookie },
});
expect(res.status).toBe(403);
});
One such test per sensitive endpoint is cheap. It also protects you from the next AI-generated refactor that "simplifies" the handler.
This is check 1 and check 6 in our security checklist for vibe-coded apps. If you want every endpoint replayed and fixed for you, see our app rescue and security audit.
