A secure Node.js authentication server implementation using JSON Web Tokens (JWT) with refresh token functionality.
- User registration and login
- JWT-based authentication
- Refresh token mechanism
- HTTP-only cookies for token storage
- Rate limiting
- Security headers with Helmet
- Request logging
- Node.js (v14 or higher)
- npm or yarn
- Clone the repository
- Install dependencies:
npm install- Create a
.envfile in the root directory with:
JWT_SECRET=your_jwt_secret_here
REFRESH_TOKEN_SECRET=your_refresh_token_secret_here
PORT=3000
Development mode:
npm run devProduction mode:
npm startRegister a new user.
{
"username": "user123",
"password": "password123"
}Login with existing credentials.
{
"username": "user123",
"password": "password123"
}Logout and invalidate tokens.
Get a new access token using refresh token.
Example protected route (requires authentication).
- Access tokens expire after 15 minutes
- Refresh tokens expire after 30 days
- HTTP-only cookies prevent XSS attacks
- Rate limiting prevents brute force attacks
- Helmet middleware adds security headers
- Password hashing using bcrypt
- CSRF protection with SameSite cookies
- express
- jsonwebtoken
- bcryptjs
- cookie-parser
- dotenv
- express-rate-limit
- helmet
- morgan