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.
- 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. - 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.
- Confirm
user_rolesand ahas_role()function already exist (see how-do-i-create-different-user-roles). - Ask Lovable to add the route guard using that function.
- 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.roleinstead ofuser_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
- AnswerHow do I add password reset?Ask Lovable to add a "forgot password" flow — it uses Lovable Cloud's authentication to email a reset link and lets the user set a new password.
- AnswerHow do I create admin users?Add a `user_roles` table with an `admin` role, assign it to the specific user's ID, and check that role server-side (in RLS policies and any admin-only functions) rather than trusting a front-end flag.
- AnswerHow do I add login to Lovable?Ask Lovable to add authentication — it enables Lovable Cloud (if not already on), then generates sign-up/login pages and wires them to email, Google, or other providers.
- AnswerWhy does my app keep redirecting to login?This is almost always a session or route-guard bug: either the auth session isn't persisting, or a protected route checks auth state before it's loaded, so it bounces back to login even when signed in.
You might also need
- GuideHow to Create Protected PagesRestrict routes to signed-in users properly — including the server-side check most AI-generated guards miss.
- GuideHow to Add User Login to a Lovable ProjectSign-up, sign-in, sign-out and sessions — plus the settings people forget that break login on the live site.
- GuideHow to Add User Roles and PermissionsAdmin, editor and member roles done safely — stored separately from user profiles so they cannot be escalated.