Secure authentication API template using Node.js, TypeScript, PostgreSQL, Prisma, JWT, and Nodemailer.
- Node.js
- TypeScript
- Express.js
- Prisma ORM
- JWT
- PostgreSQL
- Docker
- Nodemailer
- Handlebars - Email template engine
- Zod - Data validation
- Jest - Unit testing
- bcryptjs - Password hashing
- ✅ User registration and login with JWT token generation for secure sessions
- 🔄 Refresh Token system for automatic session renewal
- 🔐 Secure password storage using Bcrypt with salt and hash
- ✉️ Advanced email system with Handlebars templates, queue with retry logic, and preheader support
- 🔄 Password recovery via temporary code sent by email
- 🛡️ JWT protection middleware for authenticated routes
- 🔒 Logout and bulk logout with token revocation
- ⏱️ Configurable expiration control for tokens and reset codes
- 🚫 Rate limiting for spam prevention in password reset
- 📊 Attempt tracking and security logs
- 🧪 Comprehensive unit tests for all features
- Duration: 15 minutes
- Usage: Authentication in requests
- Storage: Client (localStorage, cookies, etc.)
- Duration: 7 days
- Usage: Automatic access token renewal
- Storage: Database with device tracking
- Security: Individual and bulk revocation
git clone https://github.com/SamuelPSantiago/auth-api-template.git
cd auth-api-template
Create a .env file based on the example:
# Environment
NODE_ENV="development" or "production"
# Database
DATABASE_URL="postgresql://user:password@db:5432/mydb"
# JWT Secrets
JWT_SECRET="your_access_token_secret_key"
JWT_REFRESH_SECRET="your_refresh_token_secret_key"
# Email Configuration
EMAIL_HOST="smtp.yourprovider.com"
EMAIL_PORT="587"
EMAIL_USER="email@domain.com"
EMAIL_PASS="password"
EMAIL_FROM_NAME="Sender Name"
EMAIL_FROM_ADDRESS="email@domain.com"
EMAIL_SECURE="false" # true for 465, false for other ports
EMAIL_MAX_CONNECTIONS="5"
EMAIL_MAX_MESSAGES="100"
EMAIL_MAX_RETRIES="3"
EMAIL_RETRY_BASE_DELAY_MS="500"
EMAIL_QUEUE_INTERVAL_MS="200"
# Security Settings
BCRYPT_ROUNDS="12"
RESET_TTL_MINUTES="15"
MAX_REQUESTS_PER_HOUR_PER_EMAIL="3"
MAX_VERIFICATION_ATTEMPTS="5"
| Script | Description |
|---|---|
| npm run dev | Starts the API in development mode |
| npm run build | Compiles TypeScript files |
| npm run start | Starts the API in production (dist/) |
| npm run test | Runs unit tests |
| npm run test:watch | Runs tests in watch mode |
| npm run prisma:studio | Database GUI interface |
| npm run prisma:migrate | Runs database migrations |
The project is organized in a modular and scalable way, separating responsibilities by domain:
src/
├── controllers/
│ ├── auth.controller.ts # Authentication controller (register, login)
│ ├── refreshToken.controller.ts # Refresh token controller
│ └── passwordReset.controller.ts # Password reset controller
│
├── email/
│ ├── templates/ # Handlebars email templates
│ │ ├── passwordReset.html # Password reset template with preheader
│ │ └── register.html # Welcome template with preheader
│ └── index.ts # Email service with Handlebars rendering
│
├── lib/
│ └── prisma.ts # Configured Prisma client
│
├── middlewares/
│ └── auth.middleware.ts # JWT authentication middleware
│
├── routes/
│ ├── auth.routes.ts # Authentication routes
│ ├── refreshToken.routes.ts # Refresh token routes
│ └── passwordReset.routes.ts # Password reset routes
│
├── services/
│ ├── emailService.ts # Email sending service with validation
│ ├── emailQueue.ts # Email queue with retry logic and backoff
│ └── refreshTokenService.ts # Refresh token management service
│
├── types/
│ ├── auth.ts # Authentication-related types
│ └── email.ts # Email-related types
│
├── utils/
│ ├── generateToken.ts # Token generation and verification functions
│ ├── passwordReset.ts # Password reset utilities
│ └── cleanupTokens.ts # Automatic cleanup of expired tokens
│
└── index.ts # Main application entry point
POST /auth/register- User registrationPOST /auth/login- User login
POST /auth/refresh- Renew access tokenPOST /auth/logout- Logout (revokes refresh token)POST /auth/logout-all- Bulk logout (revokes all tokens)
POST /auth/request-reset- Request password resetPOST /auth/verify-code- Verify reset codePOST /auth/reset-password- Set new password
- Handlebars for secure variable interpolation with automatic escaping
- Preheader support for better email client preview
- Template caching in production, hot-reload in development
- Automatic text generation from HTML content
- In-memory queue with configurable processing interval
- Exponential backoff retry strategy with jitter
- Configurable retry limits and delays
- Error logging with detailed failure information
- SMTP support with connection pooling
- Development mode with JSON transport for testing
- Flexible authentication (optional for some providers)
- Environment-based configuration
- Maximum 3 reset requests per hour per email
- Maximum 5 code verification attempts
- Reset codes expire in 15 minutes (configurable)
- IP and User-Agent for each request
- Verification attempt history
- Token revocation logs
- Password versioning
The project is ready to run with Docker and Docker Compose. To do this, simply run:
docker compose build
docker compose up
This will start the API and the PostgreSQL database automatically, with data persistence configured. Make sure to correctly fill in the .env file before starting the containers.
The project includes a comprehensive unit test suite:
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch- ✅ Authentication controllers
- ✅ Refresh token controllers
- ✅ Password reset controllers
- ✅ Authentication middlewares
- ✅ Validation utilities
Samuel Pinheiro Santiago
Full Stack Developer - LinkedIn