This repository contains a minimal Node.js RESTful microservice that implements user authentication and authorization using Express, MongoDB (Mongoose), bcrypt, and JWT.
Features
- Register and login with email/password
- Password hashing with bcrypt
- JWT access tokens
- Role-based authorization (user/admin)
- Protected route example
- Ready for Docker
Quick start
- Copy .env.example to .env and set MONGO_URI and JWT_SECRET.
- npm install
- npm run dev
API Endpoints
- POST /api/auth/register { name, email, password }
- POST /api/auth/login { email, password }
- GET /api/auth/me (protected)
- GET /api/users (protected, admin only)
Environment variables (.env)
- MONGO_URI - MongoDB connection string
- JWT_SECRET - secret used to sign JWTs
- PORT - optional server port (default 3000)
Docker
- Build: docker build -t nodejs-microservice .
- Run: docker run -e MONGO_URI="..." -e JWT_SECRET="..." -p 3000:3000 nodejs-microservice