BackendJWTAuthSecurityNode.js

Understanding JWT: Authentication Done Right

JSON Web Tokens are everywhere, but they are also misused. Learn how they work, and how to use them securely.

Abdur Razzak

Abdur Razzak

Full-Stack Web Developer

July 30, 2025 8 min read

JWT Structure

A JWT has three Base64URL-encoded parts separated by dots: header, payload, and signature. The header specifies the algorithm. The payload carries claims — user ID, roles, expiry. The signature verifies the token was not tampered with.

Signing and Verifying

Sign tokens on the server with a secret (HMAC) or a private key (RSA/ECDSA). Verify them in middleware by checking the signature and the expiry claim. A valid signature does not mean the user is authorised — always check roles and permissions too.

Common Security Mistakes

Storing JWTs in localStorage exposes them to XSS attacks. Use httpOnly cookies instead. Never put sensitive data in the payload — it is Base64-encoded, not encrypted, and can be decoded by anyone. Always set a short expiry and use refresh tokens.

Refresh Token Strategy

Issue short-lived access tokens (15 minutes) and long-lived refresh tokens (7 days) stored in httpOnly cookies. When the access token expires, the client silently exchanges the refresh token for a new one. Rotate refresh tokens on each use to detect theft.

Share this article

All posts
#JWT#Auth#Security#Node.js
Abdur Razzak — Full Stack Web Developer
Available for projects

Need a React or Next.js Developer?