Sub topic 12
Accounts, authentication and access control
This chapter assumes you have a data model with an owner field on user-created records (see the data modeling chapter). It also assumes you use a builder with a hosted backend such as Supabase, or one with built-in user accounts.
Authentication versus authorization
Authentication answers "who is this user?" Authorization answers "what may this user do?" Builders handle the first well. The second is where MVPs leak data.
| Concept | Question | Example |
|---|---|---|
| Authentication | Who are you? | Signing in with email and a magic link |
| Authorization | What can you touch? | Reading only your own tasks |
Pick the simplest sign-in method
For an MVP, choose one method and skip the rest.
- Email magic link or one-time code: no passwords to store or reset.
- Social login (Google): fewer steps, but it needs provider setup.
- Email and password: familiar, but you must support password reset.
Use your platform's built-in auth. Do not build your own.
The access model
Most MVPs need only two rules: signed-out visitors see public pages only, and signed-in users see and change their own records. Add an admin role later, when a real need appears.
Enforce rules in the database
Hiding a button in the interface is not access control. A user can still call your backend directly. The rule must be enforced where the data lives. On Postgres, row-level security (RLS) does this.
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);using filters which existing rows a user can see or change. with check validates the new values being written. The update policy needs both, so a user cannot reassign a row to someone else.
Common errors
- RLS is off, or a table has no policies. With RLS off, anyone with the public key can read every row. With RLS on and no policies, nobody can read anything, which breaks the app.
- A generated app puts a service or admin key in browser code. That key bypasses RLS. Keep it on the server only.
- Policies cover select but not update or delete.
- Testing only as yourself. Always test with two accounts.
Verify with two accounts
- Create accounts A and B.
- As A, create a task.
- As B, load the list. A's task must not appear.
- As B, try to open or edit A's task by its ID. The request must fail.
- Sign out and repeat. Nothing should be returned.
Exercise
Your AI builder generated a task list that filters by the signed-in user in the interface, but the tasks table has RLS disabled. Why is this unsafe, and what do you change?
Show answer
The filter runs in the browser, so anyone can call the backend directly and read every row. Enable RLS on the table and add policies, such as select using (auth.uid() = owner_id), plus matching insert and update policies. Then retest with two accounts.