JWT Utils Function
JWT Utils Function provides a production-ready authentication utility based on access tokens and refresh tokens for Express-based applications.
It is designed to be:
- Secure by default
- Simple to integrate
- Framework-agnostic
- Compatible with MVC, feature-based architectures.
Installation Guide
Install the component using the Servercn CLI:
npx servercn-cli add jwt-utilsWhat Is JWT?
A JSON Web Token (JWT) is a compact, URL-safe token used to securely transmit claims between a client and a server.
A JWT consists of three parts:
1. Header
Describes the signing algorithm and token type:
2. Payload
Contains claims (data). In Servercn, this typically includes the user identifier:
3. Signature
Ensures the token has not been tampered with. It is generated using your secret key.
(Access vs Refresh) Token
Servercn follows a dual-token strategy, which is the recommended approach for production systems.
1. Access Token
- Short-lived (e.g. 15m)
- Sent with every authenticated request
- Used for authorization
- Stored in memory or HTTP-only cookies
2. Refresh Token
- Long-lived (e.g. 7d)
- Used only to issue new access tokens
- Should be stored securely (HTTP-only cookie or database)
- Can be revoked or rotated
Token Flow Overview
1. User logs in: Server generates an access token and a refresh token
2. Client sends access token: Included in the Authorization header
3. Access token expires: Client sends refresh token to /auth/refresh
4. New access token issued: Session continues without re-login
Prerequisites
Ensure the following environment variables are defined before running the application:
JWT_ACCESS_SECRET: Used to sign and verify access tokens
JWT_REFRESH_SECRET: Used to sign and verify refresh tokens - These secrets must be different and kept private
Ensure the following configuration are defined:
src/configs/env.ts
Basic Implementation
1. MVC Structure
src/utils/jwt.ts
2. Feature Structure
src/modules/auth/jwt.helpers.ts
Usage Example
Best Practices
Keep Payloads Small
Avoid storing large objects or sensitive personal information (PII) in the JWT payload. The token is sent with every request, so keeping it small reduces bandwidth usage and improves performance.
- Do: Store only essential identifiers (e.g.,
userId,role). - Don't: Store entire user profiles, preferences, or large arrays.
Secure Storage
- Access Tokens: Store in memory (for single-page apps) or HTTP-only cookies if possible to mitigate XSS attacks.
- Refresh Tokens: Store in HTTP-only, Secure, SameSite cookies to prevent XSS and CSRF attacks.
Token Rotation
Implement refresh token rotation to detect token theft. If a refresh token is reused, invalidate the entire token family for that user.