Sub topic 12
Accounts, authentication and access control
This chapter assumes you have a data model with an owner field on user-created records (from the data modeling chapter) and a working core flow. You should know what a web form, a session and a database table are. By the end you will be able to separate authentication from authorization, pick a sign-in method, and enforce "users see only their own data" in the database itself.
Authentication, authorization and access control
Three terms are often mixed up.
- Authentication answers "who is this?" It is the sign-in step.
- Authorization answers "what may this person do?" It is the rule applied after sign-in.
- Access control is the set of authorization rules your app enforces, such as "a user reads only their own tasks."
Most no-code and AI-built MVPs get authentication right, because the platform provides it. They get authorization wrong, because the rules are specific to your data and nobody writes them unless you ask.
Choosing a sign-in method
For an MVP, use the method that gets a real user into the app with the fewest steps and the least code you must maintain.
| Method | Friction for the user | Risk and upkeep for you | Good fit |
|---|---|---|---|
| Email and password | Medium (remember a password, reset flow) | Platform stores password hashes. You depend on its reset and rate limiting. | General use |
| Magic link (one-time email link) | Low (no password) | Depends on reliable email delivery. Links expire and can land in spam. | Low-frequency tools |
| Social sign-in (Google, Apple) | Lowest | Needs provider setup and approved redirect URLs. Some users lack or avoid the account. | Consumer apps |
| Single sign-on (SSO) | Low for company staff | Often a paid tier. Setup is slow. | Selling to companies later |
Start with one or two methods. Do not build your own password storage. Use the platform's built-in auth service, such as Supabase Auth, Firebase Auth, Clerk or the auth module of your no-code tool.
How a signed-in request works
After sign-in, the platform gives the browser a session token. The browser sends it with every request. The backend reads it to learn the user's id, then applies your rules.
The key point is where the rule runs. If only the screen hides other people's data, anyone who sends a request directly can still read it. The rule must run on the server or in the database.
Enforcing ownership with row-level security
Row-level security (RLS) is a Postgres feature that attaches rules to a table. The database applies them to every query, whichever screen or script sent it. Supabase, a common backend for AI builders, exposes it directly. This table from the earlier prompt example stores owner_id and limits each user to their own rows:
create table tasks (
id uuid primary key default gen_random_uuid(),
owner_id uuid not null references auth.users(id),
title text not null,
client_name text,
due_date date,
status text not null default 'todo' check (status in ('todo', 'doing', 'done')),
created_at timestamptz not null default now()
);
alter table tasks enable row level security;
create policy "owners read own tasks" on tasks
for select using (auth.uid() = owner_id);
create policy "owners insert own tasks" on tasks
for insert with check (auth.uid() = owner_id);
create policy "owners update own tasks" on tasks
for update using (auth.uid() = owner_id) with check (auth.uid() = owner_id);Each policy covers one action:
usingfilters which existing rows the user may see or change.with checkvalidates the new row values. Without it on insert, a user could create a task owned by someone else.auth.uid()returns the id from the session token.
There is no delete policy above. With RLS on and no matching policy, the action is denied. That is the safe default, and you add a delete policy only when your core flow needs one.
Roles and shared data
Per-owner rules cover the first version of most MVPs. When you add teams or an admin, resist inventing a complex permission system. Use the smallest model that works:
- Single owner. One user owns each record. Add nothing else.
- Owner plus role. Add a
rolefield on the user profile (member,admin). Admin policies check the role. - Shared workspace. Add a
memberships(user_id, workspace_id, role)table. Records belong to a workspace, and policies check membership.
Each step adds policies to write and test. Move up only when a real user needs it.
Common errors
- Hiding instead of denying. The button is hidden in the UI, but the database still allows the action. Test by calling the API directly.
- RLS never enabled. In Supabase, a table created without
enable row level securityis open to anyone holding the public key. AI builders sometimes skip it. - Service keys in the browser. The service-role (admin) key bypasses RLS. It must never appear in front-end code or a public repository. Only the public (anon) key belongs in the browser.
- Trusting a user id sent by the client. If a form sends
owner_idand the server saves it, one user can write as another. Derive it from the token, or enforce it withwith check. - Weak recovery paths. Check the password reset and magic link flows. Links should expire and work once.
- Test accounts left behind. Remove demo admins and shared logins before launch.
A two-account test
Run this before you invite anyone:
- Create user A and user B.
- As A, create two tasks.
- As B, open the task list. It must be empty.
- As B, try to read, edit and delete A's task by id, through the app and through the API if you can.
- Sign out. Confirm the list page and the API both refuse access.
Any step that succeeds when it should fail is a bug. Fix it in the rule, not in the screen.
Asking the AI builder for the right thing
Put access rules in your prompt, as in the earlier TaskNest example ("each user can read and change only their own tasks"). Then check the generated policies against your list instead of trusting the summary the tool prints.
Exercise
Your app hides the "Delete" button on tasks that belong to other users, but a tester pastes a delete request with another user's task id into the API and the task disappears. Which layer is missing, and what is the fix?
Show answer
Authorization is missing on the server side. The UI only hides the button, so it does not stop direct requests. The fix is a database or backend rule, such as a row-level security delete policy that allows the action only when auth.uid() equals owner_id (or no delete policy at all if the flow does not need deletion), followed by a re-test with two accounts.
Exercise
Why does the insert policy use with check instead of only using?
Show answer
An insert has no existing row to filter, so using does not apply. with check validates the values of the new row. It stops a user from inserting a task whose owner_id is someone else's id.