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

How do I restrict pages to logged-in users?

Last checked 31 Aug 2026

Short answer

Wrap the routes in a protected-route component that checks the current auth session and redirects signed-out users to login, and back this up with row-level security on any data those pages fetch.

Two layers of protection

Restricting a page is a front-end convenience; it stops honest users wandering into pages they shouldn't see, but it doesn't secure your data. You need both:

  1. Route guard (UI): a wrapper component that checks whether a session exists before rendering the page, redirecting to /login if not.
  2. Row-level security (RLS, data): Postgres policies on your tables that only let authenticated users read/write rows they're allowed to, regardless of what the front end shows.

How to set it up

  1. Ask Lovable: "Add authentication with email/password and protect the /dashboard route so only logged-in users can view it."
  2. Lovable will generate a guard component and wire it into your router.
  3. Ask it to add RLS policies restricting the underlying tables to auth.uid() matches, not just page-level checks.

Restrict the /account and /dashboard pages to signed-in users only, and add row-level security so signed-out requests can't read that data via the API either.

What usually goes wrong

  • Only guarding the route, leaving the database table publicly readable — anyone with the API URL can still fetch the data.
  • Guard checks running before the session finishes loading, causing a flash of redirect (see why-does-my-app-keep-redirecting-to-login).
  • Forgetting server functions/edge functions also need their own auth checks — a protected page calling an open function is still a hole.

Roles vs simple login

If you need different access levels (not just logged-in vs logged-out), that's role-based access — see how-do-i-create-different-user-roles.

Sources

Was this answer helpful?

Related questions

Website made, maintained and hosted by PromptSites.com

PromptSites logo