Authentication and Authorization
🎯 Introduction
By the end of this article, you will be able to separate identity verification from permission checks and choose the right failure response.
🪪 Authentication answers “Who are you?”
Authentication verifies whether a request represents a particular user. Login can verify a username and password; subsequent requests verify a session or token. A token is a credential supplied by the client. The server must validate it before trusting the identity it represents.
userId: 1 in a request body is not an authentication result: any caller can submit it. The current user ID must come from a server-verified session or token, rather than a URL, form, or hidden page field.
🔒 Authorization answers “What can you do?”
Authorization determines whether an operation is allowed after identity has been established. User 1 being allowed to edit their own profile does not allow them to edit user 2's profile.
Create ownership.mjs to express only the rule for editing one's own profile:
export function canEditProfile(actor, targetId) {
return actor !== null && actor.id === targetId;
}
actor is the verified current user, not a value read directly from the request body. targetId identifies the target and comes from a route parameter. The business check compares values from two different sources.
🧪 Test different identities against the same target
Create ownership-demo.mjs:
import { canEditProfile } from './ownership.mjs';
console.log(canEditProfile({ id: 1 }, 1));
console.log(canEditProfile({ id: 1 }, 2));
console.log(canEditProfile(null, 1));
Running it prints true, false, false. These objects are local test data. A real endpoint must authenticate before constructing actor; the frontend must not be allowed to supply it freely.
🚦 401 and 403 call for different frontend behavior
| Request situation | Common status | Page behavior |
|---|---|---|
| Missing, invalid, or expired credentials | 401 | Prompt login or attempt a refresh according to the contract |
| Valid identity without permission for the operation | 403 | Explain insufficient permissions without repeatedly requesting login |
| Resource existence should remain undisclosed | 404, depending on policy | Apply the same policy whether the resource exists or not |
Endpoints should authenticate, authorize, validate business parameters, then modify data. Do not complete the modification before returning 403 for insufficient permissions.
🧭 Role checks and ownership checks serve different purposes
Allowing an editor to update articles does not necessarily allow them to update everyone's articles. Rules may also restrict ownership, organization scope, or record state: an archived article may no longer be editable, for example.
Evaluate every operation against current trusted server data. Hiding frontend buttons improves interaction but does not replace backend checks. The same strict protection must apply when clients change request paths.
❓ Questions and Answers
Q1: Does successful login grant every permission?
No. Login establishes identity; each sensitive operation still needs authorization.
Q2: Is checking permissions once when the page opens enough?
No. Later operations may access different resources, and permissions may change. The server must check the actual request.
Q3: Do administrators still need field validation?
Yes. Permission to act does not guarantee valid input. Authentication, authorization, and validation are separate steps.
🧪 Practice
For “read my private file” and “administrator disables an account,” specify the identity source, permission rule, and whether failures should return 401 or 403.
// Write your code or record your verification results here
