![]() |
Secure. Scalable. Observable. |
A robust authentication microservice for modern web applications, featuring JWT, email verification, password reset, and monitoring.
Features • Tech Stack • Getting Started • Architecture • Contributing
MERN Auth Microservice is a production-ready authentication backend for web and mobile apps. It provides secure user registration, login, JWT-based sessions, email verification, password reset, and exposes metrics for monitoring. Built with Node.js, Express, and MongoDB, it includes comprehensive test coverage with Jest and Supertest, and is containerized for easy deployment.
![]() API Documentation Interactive Swagger UI for all endpoints |
||
![]() Prometheus & Grafana Dashboards Real-time monitoring endpoint |
![]() Authentication Flow JWT, Email Verification, Password Reset, Monitoring |
|
- JWT-based Login — Secure, stateless sessions
- User Registration — With hashed passwords
- Email Verification — OTP-based, with expiry
- Password Reset — Multi-step, OTP-protected
- Logout — Secure session termination
- HTTP-only Cookies — Prevent XSS attacks
- Bcrypt Password Hashing — 10 salt rounds
- Environment-based Cookie Security — SameSite, Secure flags
- Input Validation — Required fields, email normalization
- Prometheus Metrics — System and custom HTTP request metrics
- Grafana Ready — For dashboard visualization
- Jest Test Suite — Comprehensive unit and integration tests
- Supertest — HTTP API endpoint testing
- MongoDB Memory Server — In-memory database for testing
- Test Coverage — Authentication flows and endpoints
- Dockerized — Easy deployment anywhere
- Modular Codebase — Clean separation of concerns
- User Authentication Service for any web/mobile app
- Microservice in a larger architecture
- API Gateway Auth Layer
- Production-ready Auth for SaaS
- Learning/Reference for secure Node.js auth
| Technology | Purpose |
|---|---|
| Express.js | Backend Framework |
| MongoDB | NoSQL database |
| Mongoose | MongoDB ODM |
| JWT | Token-based authentication |
| bcryptjs | Password hashing |
| Nodemailer | Email sending (SMTP) |
| prom-client | Prometheus metrics |
| Technology | Purpose |
|---|---|
| Jest | Testing framework |
| Supertest | HTTP assertion library |
| MongoDB Memory Server | In-memory MongoDB for testing |
| Technology | Purpose |
|---|---|
| Docker | Containerization |
| Prometheus | Monitoring |
| Grafana | Metrics visualization |
┌──────────────────────────────────────────────┐
│ Client (Frontend) │
│ (Any web/mobile app, Postman, etc.) │
└──────────────────────────────────────────────┘
│ HTTPS/REST
▼
┌──────────────────────────────────────────────┐
│ MERN Auth Microservice (Node.js) │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ Express.js │ │ Prometheus │ │
│ │ API Server │ │ Metrics │ │
│ └──────────────┘ └──────────────┘ │
│ │ │ │
│ ▼ ▼ │
│ MongoDB Atlas /metrics endpoint │
└──────────────────────────────────────────────┘
- Node.js (v18 or higher)
- npm
- MongoDB (local or Atlas)
-
Clone the repository
git clone https://github.com/karanhimadri/mern-authentication-microservice.git cd mern-authentication-microservice/server -
Install dependencies
npm install
-
Configure environment variables
- Copy
.env.exampleto.envand fill in your values:MONGO_URI=your_mongodb_connection_string JWT_SECRET=your_jwt_secret SMTP_USER=your_smtp_user SMTP_PASS=your_smtp_pass SENDER_EMAIL=your_email@example.com NODE_ENV=development PORT=4000
- Copy
npm start
# Server runs on http://localhost:4000Run the test suite to ensure everything is working correctly:
npm test
# Runs all tests with JestThe test suite includes:
- Unit Tests: Authentication controller functions
- Integration Tests: API endpoint testing with Supertest
- Mocked Services: Email service, JWT, and database operations
Test files are located in the /test directory:
auth.test.js- Authentication endpoint testsapp.test.js- Application setup tests
docker build -t mern-auth-microservice .
docker run -p 8080:4000 --env-file .env mern-auth-microservice
# App will be available at http://localhost:8080Himadri Karan
Backend Developer & Business Solutions Specialist
- 📧 Email: Karanhimadri1234@gmail.com
- 💼 LinkedIn: linkedin.com/in/himadrikaran
- 🌐 Portfolio: Himadri.me
- 🐙 GitHub: github.com/karanhimadri
Made with ❤️ and lots of ☕
If you found this project helpful, please give it a ⭐️!



