Authentication proves who the user is; authorization decides what they may do. In JavaScript apps: authenticate with a proven protocol or provider (OAuth 2.0/OpenID Connect with Authorization Code + PKCE, or a well-built session login), support passkeys/MFA, store sessions in HttpOnly; Secure; SameSite cookies (or keep tokens server-side via a backend-for-frontend), and enforce authorization on the server for every request. The front end only reflects permissions in the UI; it can never be the gatekeeper.
Server sessions (classic, robust)
POST /login → Set-Cookie: __Host-sid=<random id>; Path=/; Secure; HttpOnly; SameSite=Lax
The server keeps session state (Redis/DB). Easy revocation, no token handling in JS. Needs CSRF protection for state-changing requests (SameSite + tokens/Origin checks).
OAuth 2.0 / OpenID Connect
state (CSRF) and nonce (replay) parameters; libraries (oidc-client-ts, Auth.js, provider SDKs) handle this.Tokens in a pure SPA (when no BFF)
HttpOnly cookie scoped to the token endpoint.localStorage for long-lived credentials: any XSS can read it.JWT caveats
exp, iss, aud on the server; pin the algorithm (reject alg: none and unexpected algorithms).const credential = await navigator.credentials.get({
publicKey: optionsFromServer, // challenge, rpId, allowCredentials
});
await fetch('/webauthn/verify', { method: 'POST', body: JSON.stringify(serialize(credential)) });
Enforce on the server, close to the data:
app.patch('/api/projects/:id', requireAuth, async (req, res) => {
const project = await db.project.findUnique({ where: { id: req.params.id } });
if (!project) return res.sendStatus(404);
if (!can(req.user, 'update', project)) return res.sendStatus(403); // object-level check
// validate body, then update…
});
function can(user, action, project) {
if (user.role === 'admin') return true;
return action === 'update' && project.ownerId === user.id;
}
/orders/123 to /orders/124). Broken access control is the top OWASP risk.{can(user, 'delete', doc) && <button onClick={remove}>Delete</button>}
<Route path="/admin" element={<RequireRole role="admin"><Admin /></RequireRole>} />
Route guards and hidden buttons are UX only. Code and data shipped to the client are visible, so don't ship admin-only data to non-admins either. Handle 401 (redirect to login, preserving a validated return URL) and 403 (explain, don't loop).
HTTPS + HSTS, strict CSP (XSS undermines every client-side auth measure), CSRF protection for cookie sessions, rate limiting, audit logs for sensitive actions, and secure account recovery (recovery is often the weakest link).
Spotted a mistake or a rights concern? Report an issue