A complete JWT-based authentication system built with TypeScript, Express.js, and SQLite.
- User signup and login with JWT tokens
- Multiple JWT algorithms support (HMAC: HS256/384/512, RSA: RS256/384/512)
- Customizable token expiration duration
- Optional refresh token functionality
- Secure password hashing with scrypt
- Protected routes with JWT middleware
- Custom JWT implementation from scratch
- Support for both HMAC and RSA algorithms
- Base64URL encoding/decoding utilities
- Comprehensive token validation
- SQLite database with users and refresh_tokens tables
- Automatic database initialization
- Proper indexing and foreign key constraints
The API implements a secure refresh token system with the following features:
- Optional Refresh Tokens: Only issued when
issueRefreshToken: trueis provided during login - Separate Secret: Refresh tokens are signed with a different secret (
REFRESH_TOKEN_SECRET) than access tokens - Smart Expiration: Refresh tokens expire 30x longer than access tokens, with a minimum of 30 minutes
- JWT-based: Refresh tokens are proper JWT tokens, not random strings
- Token Rotation: Refresh tokens are rotated on each use for enhanced security
- Database Tracking: Tokens are stored in the database for revocation support
- If access token expires in 1 hour → refresh token expires in 30 hours
- If access token expires in 15 minutes → refresh token expires in 30 minutes (minimum)
- If access token expires in 24 hours → refresh token expires in 30 days
- Refresh tokens use
HS256algorithm with separate secret - Token hashes stored in database for secure validation
- Automatic cleanup of expired tokens
- Multi-device logout support
- Token rotation prevents replay attacks
JWT_SECRET='your-access-token-secret'
REFRESH_TOKEN_SECRET='your-refresh-token-secret'POST /signup- Register a new userPOST /login- Authenticate user and get access tokenPOST /refresh- Refresh access token using refresh tokenPOST /logout- Logout from current devicePOST /logout-all- Logout from all devicesGET /algorithms- Get available JWT algorithms
GET /secret- Get protected secret message (requires authentication)
POST /api/auth/login
{
"username": "john_doe",
"password": "securePassword123",
"algorithm": "HS256",
"expiresIn": "1h",
"issueRefreshToken": true
}Note: Set issueRefreshToken: true in the login request to receive a refresh token. If not specified or set to false, only an access token will be issued.
- nvm install 23
- nvm use 23
- npm install
- npm run dev
- HMAC
- Utils Segregation
- RSA
- API Expose
- Database for storing users' credentials and secret message for testing purposes
- Secret message route
- Refresh Token Functionality
- Optional refresh token issuance based on user preference
- Frontend Setup
- Frontend Integration