How do I create different user roles?
Last checked 31 Aug 2026
Short answer
Store roles in a dedicated `user_roles` table (never a column on the user's profile), and use a security-definer function to check roles inside RLS policies and route guards.
Why roles need their own table
If role lives on the same row a user can edit (like profiles), a bug or a crafted request can let someone grant themselves admin. Lovable's security guidance is explicit: roles must live in a separate user_roles table, keyed by user_id, never a column on a profile/users row.
Setting it up
- Ask Lovable: "Create a user_roles table with roles admin, editor and user, and a has_role() function I can use in RLS policies."
- Lovable generates the table, an enum for role names, and a
security definerfunction that safely checks a user's role without triggering recursive RLS issues. - Use that function both in database policies (who can read/write what) and in the front end (which nav items/pages to show).
| Role | Typical access |
|---|---|
| admin | Full read/write, manage users |
| editor | Create/edit content, no user management |
| user | Own data only |
Add a "editor" role that can create and edit blog posts but can't manage users or see the admin dashboard.
What usually goes wrong
- Checking roles only client-side, so the restriction is cosmetic — always back it with RLS.
- Forgetting default role assignment on signup (new users should get a
userrow automatically, usually via a trigger). - Multiple roles per user not being planned for — decide upfront whether a user can hold more than one role.
See how-do-i-create-admin-users for assigning the admin role specifically, and how-do-i-build-an-admin-dashboard for using roles in a UI.
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 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.
- 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.
- 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.