Skip to content

A robust, production-ready authentication microservice system built with the MERN stack, featuring secure user registration, login, email verification, and password reset functionality with complete Swagger API Documentation.

Notifications You must be signed in to change notification settings

karanhimadri/mern-authentication-microservice

Repository files navigation

🔐 MERN Authentication Microservice

A robust, production-ready authentication microservice system built with the MERN stack, featuring secure user registration, login, email verification, and password reset functionality with complete Swagger API Documentation.

Node.js Express.js MongoDB JWT Swagger

📚 API Documentation

🚀 Interactive API Documentation: http://localhost:4000/api-docs

Complete Swagger UI documentation with:

  • Interactive API testing interface
  • Request/response schemas and examples
  • Authentication flow documentation
  • Error response details
  • Real-time API exploration

✨ Key Features

  • Secure Authentication: JWT-based authentication with HTTP-only cookies
  • Email Verification: OTP-based email verification system
  • Password Reset: Secure password reset with email OTP
  • API Documentation: Complete Swagger UI documentation
  • Input Validation: Comprehensive data validation and sanitization
  • Security Best Practices: bcrypt password hashing, CORS protection, secure headers
  • Production Ready: Environment-based configurations and error handling
  • RESTful API: Clean, well-structured API endpoints

🛠️ Technical Stack

Backend

  • Node.js - Runtime environment
  • Express.js - Web application framework
  • MongoDB - NoSQL database with Mongoose ODM
  • JWT - Secure token-based authentication
  • bcryptjs - Password hashing and validation
  • Nodemailer - Email service integration

Documentation & Testing

  • Swagger UI - Interactive API documentation
  • swagger-jsdoc - JSDoc to Swagger conversion
  • swagger-ui-express - Swagger UI middleware

Security & Middleware

  • CORS - Cross-origin resource sharing
  • Cookie Parser - Secure cookie handling
  • Input Sanitization - Email normalization and validation
  • Environment Variables - Secure configuration management

🚀 API Endpoints

POST /api/auth/register          # User registration
POST /api/auth/login             # User login
POST /api/auth/logout            # User logout
POST /api/auth/send-verify-otp   # Send email verification OTP
POST /api/auth/verify-account    # Verify user email
POST /api/auth/send-reset-otp    # Send password reset OTP
POST /api/auth/verify-password-otp # Verify password reset OTP
POST /api/auth/reset-password    # Reset user password
GET  /api/auth/get-user-details  # Get authenticated user details

🔒 Security Features

  • Password Hashing: bcrypt with salt rounds for secure password storage
  • JWT Authentication: Secure token-based authentication with expiration
  • HTTP-Only Cookies: Prevents XSS attacks by making tokens inaccessible to JavaScript
  • CORS Protection: Configured for specific origins with credentials support
  • Input Sanitization: Email normalization and comprehensive validation
  • Environment-based Security: Production-ready security configurations

🏗️ Project Structure

server/
├── config/
│   ├── mongodb.js      # Database configuration
│   ├── nodemailer.js   # Email service setup
│   └── swagger.js      # Swagger documentation config
├── controllers/
│   └── authController.js # Authentication logic
├── middleware/
│   └── userAuth.js     # JWT verification middleware
├── models/
│   └── userModel.js    # User schema and model
├── routes/
│   └── authRoute.js    # API route definitions with Swagger docs
└── server.js           # Application entry point

⚡ Quick Start

  1. Clone the repository

    git clone https://github.com/karanhimadri/MERN-Authentication.git
    cd MERN-Authentication/server
  2. Install dependencies

    npm install
  3. Environment setup

    # Create .env file with:
    PORT=4000
    MONGO_URI=mongodb+srv://<username>:<password>@cluster.mongodb.net/mearn-auth
    JWT_SECRET=your_jwt_secret_key
    NODE_ENV=development
    SENDER_EMAIL=your_email@example.com
    SMTP_USER=your_smtp_user@smtp-provider.com
    SMTP_PASS=your_smtp_password
  4. Start the server

    npm start
  5. Access API Documentation

    Open your browser and visit: http://localhost:4000/api-docs
    

🎯 Professional Highlights

  • Scalable Architecture: Modular design following MVC pattern
  • Production Deployment: Docker containerization and environment configurations
  • Code Quality: Clean, maintainable code with proper error handling
  • Security First: Implementation of industry-standard security practices
  • API Design: RESTful endpoints with consistent response formatting

📧 Contact

Karan Himadri
Full Stack Developer
📧 karanhimadri@email.com
🔗 LinkedIn
🐱 GitHub


This project demonstrates proficiency in modern web development practices, secure authentication implementation, and full-stack development capabilities.

About

A robust, production-ready authentication microservice system built with the MERN stack, featuring secure user registration, login, email verification, and password reset functionality with complete Swagger API Documentation.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published