Skip to content
Every tutorial here is 100% free — and always will be
LSLovableStudent

How do I prevent normal users accessing admin pages?

Last checked 31 Aug 2026

Short answer

Guard the admin route with a check against the user's role from your `user_roles` table (not a front-end-only flag), and mirror that same check in RLS policies on any data the admin page loads.

Two places to enforce it

Blocking the page in the UI is necessary but not sufficient — the real barrier is the database.

  1. Route guard: the admin route checks has_role(auth.uid(), 'admin') (via your roles function) before rendering, redirecting anyone else to a normal page or a "not authorised" screen.
  2. RLS policies: the tables the admin page reads or writes must independently check the same role, so a non-admin can't just call the API directly and get the data.

How to set it up

Make sure only users with the admin role can view /admin and can query the admin-only tables. Redirect everyone else to the home page.

  1. Confirm user_roles and a has_role() function already exist (see how-do-i-create-different-user-roles).
  2. Ask Lovable to add the route guard using that function.
  3. Ask Lovable to review RLS policies on every table the admin page touches, confirming they also require the admin role.

What usually goes wrong

  • Hiding an "Admin" nav link but leaving the route itself reachable by typing the URL directly.
  • Checking role once on page load and caching it in local state, so a role change doesn't take effect until next login — usually fine, but worth knowing.
  • RLS policies referencing profiles.role instead of user_roles, which reopens the self-promotion risk described in how-do-i-create-admin-users.

Testing

Create a second, non-admin test account and confirm it's actually blocked from both the page and the underlying API calls, not just the nav link.

Sources

Was this answer helpful?

Related questions

Website made, maintained and hosted by PromptSites.com

PromptSites logo