Authorization in the API and the UI
Enforce who may do what on the server, and only mirror it in the UI: ownership checks, roles, 401 versus 403 versus 404, and why a hidden button is not a lock.
Authorization in the API and the UI
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Authorization is decided on the server for every request, and hiding a button in the UI only improves the experience for honest users.
- The UI should show or hide controls based on permissions the server tells it about, rather than reimplementing the rules in the client.
- Use 401 when the caller is not signed in, 403 when they are signed in and not allowed, and 404 when even confirming the thing exists would leak information.
- Checking authentication alone is not enough, because the most common bug is a signed-in user reading another user's record by changing an id in the URL.
- Keep permission checks in one named function or layer so a new endpoint cannot quietly forget them, and put the data access behind it.
Authentication is not enough
Once sign-in works, there is a tempting mistake: add a check that says “is there a valid session?” and consider the endpoint protected. That stops anonymous visitors. It does nothing about signed-in users who change an id.
Taskboard’s board lives at /api/projects/1/board. Alice owns project 1. Bob is signed in and types /api/projects/1/board into his address bar. With only an authentication check, Bob sees Alice’s board. This bug has a name, broken object-level authorization (BOLA, sometimes called IDOR), and the OWASP API security top 10 puts it at number one for a reason: it is easy to write and invisible when you test with one account.
So authorization has two questions to ask each request: who is this? and may they do this to this specific record?
The check goes next to the data
Here is the handler’s first step, from the running app:
const p = db.prepare("select * from projects where id=?").get(c.req.param("id"));
if (!p || p.owner_id !== c.get("userId"))
throw new ApiError(404, "not_found", "Project not found");
I signed up two users and had the second request the first’s project. The response was {"error":{"code":"not_found","message":"Project not found"}} [404]. The owner got the board.
Consider the three status codes you could pick:
- 401 means “I don’t know who you are.” Sign in and try again. Used for a missing or expired session.
- 403 means “I know who you are, and no.” Fine when the resource’s existence is not secret, such as an admin screen or a known public project.
- 404 means “there is nothing here for you.” Use it for other people’s private records, so an attacker cannot probe which ids exist.
Pick per resource, and write it down. For Taskboard, a project the caller does not belong to is 404; an action on a project they do belong to but lack a role for is 403.
Roles on top of ownership
Ownership is the simplest rule. Real task boards have members with roles: owner, editor, viewer. The check becomes a lookup:
function can(userId: number, projectId: number, action: "read" | "edit" | "delete") {
const role = memberRole(userId, projectId); // null if not a member
if (!role) return false;
const allowed = { viewer: ["read"], editor: ["read", "edit"], owner: ["read", "edit", "delete"] };
return allowed[role].includes(action);
}
(This sketch shows the shape; I ran only the ownership version above.) Two habits keep it safe:
- One function decides. Every endpoint calls
can(...)or a helper built on it. If each handler writes its ownif, one will be wrong. - Check before the data leaves the database layer, not after. A query like
select ... where project_id = ? and member_id = ?makes an unauthorized row impossible to return, instead of relying on a laterifthat someone might reorder.
Authorization models: RBAC, ABAC and beyond compares role-based schemes with attribute-based rules, and when each outgrows the other. For a task board, roles per project are plenty.
The UI mirrors, it does not enforce
Now the browser side. If viewers cannot edit, they should not see an Edit button, because a button that fails with an error is a bad experience. That is the UI’s job: to reflect the rules. It is never the lock.
The trap is the UI deciding by itself. If the client has its own copy of if (user.role === "owner"), you now have two sets of rules, and they drift. Better: the server says what is allowed, and the UI draws it.
{"id": 7, "title": "Ship", "status": "doing", "can": { "edit": true, "delete": false }}
The component renders {task.can.edit && <EditButton />}. When the role changes, the next fetch changes the buttons, without a client release. And when someone ignores the UI and sends DELETE directly, the server runs can(...) again and answers 403. The permission is computed on the server twice, once to describe and once to enforce.
Route guards in the router work the same way. A /admin route that redirects non-admins is polite, and the API behind it must still refuse. The page’s JavaScript is delivered to everyone, including the code that renders admin screens, so assume anything in the bundle is public. For the frontend half, see authentication patterns for frontend apps.
Where checks belong by layer
| Layer | Role |
|---|---|
| Browser UI | Show or hide controls from server-provided permissions |
| Router guards | Redirect for good experience, never for security |
| API handler | Authenticate, then authorize against the specific record |
| Query | Filter by owner or membership so unauthorized rows cannot come back |
| Database | Optionally, row-level security as a second lock |
The last row is optional but worth knowing: Postgres can enforce row-level policies itself, so a bug in one handler cannot leak rows. It is a second lock, not a replacement for the first.
Test it with two accounts
The only reliable test of authorization is a second user. For every endpoint that takes an id, write a test that signs in as user B and requests user A’s record, and expect 404 or 403. Reading a handler and agreeing with it proves little; running it as the second account is what shows the check works. The integration tests lesson in the testing group turns this into an automated test.
What the video covers
ByteGrad’s “Handle Permissions Like A Pro - Every Developer Should Know This” is about 21 minutes, published March 2025. Its description says it covers the critical things to understand about permissions (authorization), and it is sponsored by Permit and carries a paid ambassador note for Kinde, so expect product mentions. I could not read a transcript, so I cannot say which models it presents. It is the closest focused video I found for the topic. Web Dev Simplified’s permissions video, about 37 minutes and also sponsor-supported, is linked as a longer alternative.
Where this fits
The server decides, and the UI reflects. The last piece of auth is the browser-side attacks that try to slip past all this by borrowing your cookie or running their script in your page. Next: CSRF, XSS and cookie settings across layers.
Check yourself
Why is hiding the Delete button not authorization?
Anyone can send the DELETE request directly, so only a check on the server can actually stop it, and the hidden button is just a convenience.
When should the API return 404 instead of 403?
When confirming that the resource exists would itself be a leak, such as another user's private project, so you answer the same way as for a missing one.
What happened when the second account requested the first account's board in my run?
It got a 404 with the not_found code, the same response as for a project id that does not exist.
How should the UI know whether to show an Edit button?
The server includes what the caller may do with each resource in its response, and the UI renders from that, while the server still re-checks when the edit request arrives.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.