How Do You Add Login and Permissions Without Holes?
Use a managed login service such as Supabase Auth or Firebase Authentication instead of writing your own. Then make the server or database check, on every request, that this user may touch this record. A login screen only proves who someone is, so the real test takes two accounts: can one reach the other's data?
Add authentication and authorization using [Supabase Auth / Firebase Authentication / my current provider]. Don't write your own password or session code. Follow these rules: 1. Verify identity on the server, for every protected route, server function and server action, from the signed token. In Supabase server code use supabase.auth.getClaims(). Never trust the user object from getSession(). 2. Take the user's ID only from the verified token. Never read it from the request body, URL or a hidden field. 3. Scope every read, update and delete to the owner, then return 404 if nothing matches: const text = 'SELECT * FROM [TABLE] WHERE id = $1 AND owner_id = $2'; const res = await client.query(text, [recordId, userId]); 4. Store roles in a dedicated table users can't write to, or in app_metadata. Never use user_metadata, and never use a column on a row the user can update. 5. Deny by default. Give me a table of every route marked public, signed-in or admin. Anything unlisted requires sign-in. 6. Use one login error for a wrong password and an unknown account: "Invalid email or password." 7. Hiding a button is not a permission check. Enforce it where the data is served. Then list each route with the line where its check happens, and write automated tests proving that user A gets 401, 403 or 404 when requesting user B's record, and that a signed-out request is refused.
- Handled by a managed provider
- Login, sessions, password resets
- Proved by a signed token
- Written for your own app
- Checked on every read and write
- Tested with two accounts
Try it now
- 1Create two test accounts. Sign in as A in a normal Chrome window and as B in a private window. As A, create a record and copy its URL. Open that URL as B. You should see a not-found or denied message.
- 2As B, press
F12, open the Network panel and save anything. Right-click the request under Name, then choose Copy and Copy as cURL. Paste it into a terminal and change the record ID to A's. This command holds B's login token, so don't paste it into a chat. - 3Run the same command with the
Authorizationand cookie headers removed, then open/adminwhile signed in as B. - 4It worked if every attempt returns
401,403,404or an empty list, and none of them returns A's data.
Use it when
As soon as two people could have different data, and before you build the features that hold it. Permissions are easier to get right when every new route starts with the check. Do the same before adding an admin page or a paid feature.
Skip it when
Skip accounts on a marketing site, a public directory, or a tool only you run on your own machine. Accounts bring password resets, email limits and support work, and if nothing is private you take on that cost for no gain.
Why it works
OWASP ranks broken access control first in its Top 10:2025. Its classic form is simple: change an ID in a URL or request and see someone else's record. OWASP's cheat sheet says random IDs help only as an extra layer, because the permission check must still run on each access.
The check has to live where a user can't change it. Lovable's docs point out that clients can call server functions directly, so running on the server doesn't make a function private. Next.js says to verify authorization inside each Server Action and not to rely on Proxy, layout or page checks alone.
Tokens matter too. Supabase warns that anyone can forge a session cookie, and that getSession() in server code doesn't revalidate it. Use getClaims(), which verifies the signature each time. Don't base permissions on user_metadata, because users can change it. Supabase says to use app_metadata instead.
Example
An invoicing app has a route, GET /api/invoices/:id. It checks that someone is signed in, then returns whichever invoice was asked for. In the two-account test, B opens A's invoice. The fix is one change to the query, shown in step 3 of the prompt, so a stranger's ID now matches nothing and the route returns 404.
Now try the privilege test on Supabase, using your own token and test project: curl -X PATCH 'https://<REF>.supabase.co/rest/v1/profiles?id=eq.<YOUR_ID>' -H "apikey: <PUBLISHABLE_KEY>" -H "Authorization: Bearer <YOUR_TOKEN>" -H "Content-Type: application/json" -d '{"is_admin": true}'. If your account becomes an admin, your update policy allows too much.
Going further
Row level security limits rows, not columns. A policy that lets users update their own profile also lets them change every column on it, including is_admin. Supabase recommends a dedicated roles table for most apps. It offers column privileges, such as grant update (title, content) on table public.posts to authenticated, as an advanced option with side effects, because select * fails on restricted columns.
Sign-in settings cause their own problems. Change Supabase's Site URL from http://localhost:3000 to your real address, or confirmation and reset emails will point to localhost. For production, Supabase recommends exact redirect URLs rather than ** wildcards. The built-in email service allows 2 auth emails an hour, and only custom SMTP changes that.
OWASP's cheat sheet treats passwords under 15 characters as weak without multi-factor login, and under 8 as weak with it. It also says to ask for the current password before changing an email or password. Supabase's leaked password protection requires the Pro plan.
Common mistakes
- Checking access only in middleware or a page layout, then leaving a server action or API route open underneath.
- Trusting a
userIdsent from the browser. Anyone can change it in the request. - Using a Firestore rule of
request.auth.uid != null. Firebase lists this as insecure, since any signed-in user can read and write the collection. - Storing a role in
user_metadata. Supabase's Security Advisor flags this as0015_rls_references_user_metadata, because users can edit that data.
Sources
- A01 Broken Access Control, OWASP Top 10:2025
- IDOR Prevention Cheat Sheet, OWASP
- Authentication Cheat Sheet, OWASP
- Security best practices, Lovable docs
- Production checklist, Next.js docs
- Server-side Auth, Supabase docs
- Row Level Security, Supabase docs
- Column-level security, Supabase docs
- Redirect URLs, Supabase docs
- Production checklist, Supabase docs
- Password security, Supabase docs
- Database advisors, Supabase docs
- Avoid insecure rules, Firebase docs
- Network features reference, Chrome DevTools docs
- Queries, node-postgres docs
Checked 6 November 2026. Products change often, so confirm details with the vendor.