A NestJS-based REST API for user registration, login, and JWT authentication.
- User registration with email and password
- Login with JWT token issuance
- Protected routes via JWT authentication
- Password hashing with bcrypt
- MongoDB storage with Mongoose
- Request validation with class-validator
- Health check endpoint
- Swagger/OpenAPI documentation at /api/docs
- Rate limiting (Throttler)
- Security headers (Helmet)
- API versioning (/api/v1 prefix)
- Node.js 18+
- MongoDB (local or remote)
npm installCopy .env.example to .env and configure:
PORT- Server port (default: 3000)MONGODB_URI- MongoDB connection stringJWT_SECRET- Secret for JWT signing (change in production)
# Development
npm run start:dev
# Production
npm run build
npm run start:prodAll endpoints are prefixed with /api/v1.
POST /api/v1/auth/register- Register (body: { email, password })POST /api/v1/auth/login- Login (body: { email, password })GET /api/v1/auth/me- Get current user (requires Bearer token)
GET /api/v1/users/:id- Get user by ID (requires Bearer token)
GET /api/v1/health- Health check
Interactive Swagger docs available at /api/docs when the server is running.
docker-compose up -ddocker build -t user-auth-api .
docker run -p 3000:3000 --env-file .env user-auth-apinpm testsrc/auth/- Authentication module (register, login, JWT)src/users/- Users modulesrc/health/- Health checksrc/common/- Shared constants
npm run start:dev- Start development server with hot reloadnpm run build- Build for productionnpm run start:prod- Run production buildnpm run test- Run unit testsnpm run test:e2e- Run end-to-end testsnpm run lint- Run ESLint
- Ensure MongoDB is running before starting the app
- Check that PORT and MONGODB_URI are set correctly in .env
UNLICENSED
Never commit .env or expose JWT_SECRET. Use strong passwords in production.
See CONTRIBUTING.md for guidelines.
See CHANGELOG.md for version history.
Use connection pooling for MongoDB in production. Consider Redis for session storage.
Health endpoint at /api/v1/health for load balancer checks.
When upgrading, run npm ci and rebuild. Check CHANGELOG for breaking changes.