Authentication & Security
JWT, sessions, password hashing, CORS, and Helmet — protect your Express APIs
Table of Contents
1. JWT (JSON Web Token)
What it is: A compact, self-contained token that carries user identity and claims (like roles or permissions). It's digitally signed, so the server can trust its data without storing session info.
How it works:
- User logs in → server creates a JWT (Header + Payload + Signature) and sends it to client.
- Client stores it (e.g., in localStorage or cookie) and sends it in the
Authorizationheader for each request. - Server verifies signature (no database lookup needed) and extracts user info.
Short Example (Node.js / Express + jsonwebtoken):
2. Sessions
What it is: Server-side storage for user data. After login, server creates a session record (usually in memory, Redis, or a DB) and sends a session ID (via cookie) to the client.
How it works:
- Client sends cookie with session ID on each request.
- Server looks up session data using that ID.
- More secure than JWT for sensitive apps (can revoke anytime) but adds server storage overhead.
Short Example (Express + express-session):
3. Password Hashing
What it is: Storing passwords as plaintext is dangerous. Hashing transforms a password into a fixed-length, irreversible string. Salting adds random data to each password before hashing, preventing rainbow table attacks.
Good hash functions: bcrypt, Argon2, PBKDF2.
How it works (bcrypt example):
- Signup: Generate a salt + hash password → store hash.
- Login: Hash the incoming password (with the stored salt) and compare to stored hash.
Short Example (Node.js + bcrypt):
4. CORS (Cross-Origin Resource Sharing)
What it is: A security mechanism that controls which domains can access your API. By default, browsers block requests from different origins (domain, port, or protocol) to prevent malicious sites from reading sensitive data.
How it works:
- Browser sends a preflight
OPTIONSrequest (for non-simple requests) asking if the server allows the actual request. - Server responds with
Access-Control-Allow-Originand other headers. - If origin is not allowed, browser blocks the response.
Short Example (Express + cors middleware):
5. Helmet
What it is: An Express middleware that sets security-related HTTP headers to protect against common web vulnerabilities (XSS, clickjacking, MIME sniffing, etc.). It's a collection of 15+ smaller middleware functions.
Key headers set by Helmet:
X-Content-Type-Options: nosniff→ prevents MIME type confusion.X-Frame-Options: DENY→ stops clickjacking via iframes.Strict-Transport-Security→ enforces HTTPS.X-XSS-Protection→ enables browser XSS filtering.
Short Example (Express + helmet):
Without Helmet → missing these headers, browser might guess MIME types or allow iframe embedding.
With Helmet → headers added automatically, hardening your app in one line.
Summary Table
| Concept | Where data lives | Stateless? | Main risk if ignored |
|---|---|---|---|
| JWT | Client (token) | Yes | Token theft, no instant revoke |
| Sessions | Server (DB/Redis) | No | Session hijacking |
| Password Hashing | Server (hash only) | N/A | Plaintext leak → full account compromise |
| CORS | Browser + Server | N/A | Unauthorized domain access |
| Helmet | HTTP headers | N/A | Missing security headers |