Skip to content

Docker-containerized MERN (MongoDB, Express, React, Node.js) authentication system with Mailtrap and Brevo integration for email delivery. It supports secure user registration, email verification, password recovery, and session management using modern technologies and cloud services.

License

Notifications You must be signed in to change notification settings

PAIshanMadusha/advanced-mern-auth-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

41 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

lock icon

Advanced Authentication System

BUILT WITH THE MERN STACK & CLIENT-SERVER ARCHITECTURE

Β Β  Β Β  Β Β 

CONTAINERIZED MICROENVIRONMENT & DEPLOYED ON DIGITALOCEAN

Β Β 


☁️ Live Demo on DigitalOcean

πŸ§ͺ Email Delivery Testing Guide:

βœ… Click the link below to access the live application and navigate to the Sign Up page.
βœ… Use your Real Email Address and Any Password to register (passwords are securely hashed).
βœ… You will receive Verification Email and other System Emails, follow the steps provided in the email.

πŸ”— Try the Advanced MERN Authentication System:
https://advanced-mern-auth-app-s552q.ondigitalocean.app

⚠️ Note: Sometimes, emails may be delivered to your Spam or Promotions folder. If you don’t see any emails in your inbox, please check those folders.


A full-stack, production-ready MERN (MongoDB, Express, React, Node.js) authentication system that supports secure user registration, email verification, password recovery, and session management. Built with a scalable architecture and containerized using Docker, it integrates Mailtrap and Brevo email services for real-time email delivery and is deployed on DigitalOcean for seamless cloud accessibility.

πŸš€ Features

  • πŸ” JWT Authentication with HTTP-only Cookies.
  • πŸ“¨ Email Verification via 6-Digit Code (Development: Mailtrap, Production: Brevo).
  • πŸ“© Welcome Emails sent after successful verification.
  • πŸ” Forgot & Reset Password flow with secure token-based links.
  • βœ… Password Strength Validation.
  • 🧠 Authentication Check API to validate active user sessions.
  • πŸ”“ Sign-Out by clearing the authentication cookie.

πŸ› οΈ Technologies Used

The following technologies are used in this project:

Technology Description
Node.js JavaScript runtime for server-side development
Express.js Minimalist web framework for Node.js
MongoDB + Mongoose NoSQL database with object modeling tool
JWT Used for stateless session authentication
BcryptJS Library for securely hashing passwords
Brevo Email API service for production email delivery
Mailtrap Email testing service for development environment
Cookie Parser Middleware to parse HTTP cookies
Crypto Built-in encryption utilities
CORS Cross-origin resource sharing support
------------------------ -----------------------------------------------------------------------------
React 19 UI library for interactive interfaces
React Router v7 Routing library for single-page React apps
TailwindCSS Utility-first CSS framework for styling
Axios Promise-based HTTP client for API requests
Framer Motion Animation library for React components
Zustand Lightweight global state management tool
Lucide-react Icon library for modern, customizable icons
Vite Fast frontend build tool and dev server

πŸ“ Project Structure

The following project structure (client-server architecture) is used in this project:

advanced-mern-auth-system/
β”œβ”€β”€ client/                    # Frontend (React + Vite)
β”‚   β”œβ”€β”€ dist/                  # Production build output
β”‚   β”œβ”€β”€ node_modules/          # Frontend dependencies
β”‚   β”œβ”€β”€ public/                # Static assets
β”‚   β”œβ”€β”€ src/                   # Source code for the React app
β”‚   β”‚   β”œβ”€β”€ components/        # Reusable UI components (Inputs, etc.)
β”‚   β”‚   β”œβ”€β”€ pages/             # Page-level components (Login, Signup, etc.)
β”‚   β”‚   β”œβ”€β”€ store/             # Zustand global state management
β”‚   β”‚   β”œβ”€β”€ utils/             # Utility functions (e.g., date handling)
β”‚   β”‚   β”œβ”€β”€ App.jsx            # Main app structure and route definitions
β”‚   β”‚   β”œβ”€β”€ main.jsx           # React root renderer
β”‚   β”‚   └── index.css
β”‚   β”œβ”€β”€ index.html
β”‚   β”œβ”€β”€ vite.config.js
β”‚   β”œβ”€β”€ tailwind.config.js     # Tailwind CSS configuration
β”‚   β”œβ”€β”€ postcss.config.js
β”‚   β”œβ”€β”€ eslint.config.js
β”‚   β”œβ”€β”€ package.json           # Frontend dependencies and scripts
β”‚   └── package-lock.json
β”œβ”€β”€ node_modules/              # Root-level dependencies (backend)
β”œβ”€β”€ server/                    # Backend (Node.js + Express)
β”‚   β”œβ”€β”€ controllers/           # Route handler logic (e.g., auth, user)
β”‚   β”œβ”€β”€ db/                    # MongoDB connection logic
β”‚   β”œβ”€β”€ mailtrap_brevo/        # Email services (Mailtrap for dev, Brevo for prod)
β”‚   β”œβ”€β”€ middleware/            # Middleware (e.g., verifyToken)
β”‚   β”œβ”€β”€ models/                # Mongoose schema definitions
β”‚   β”œβ”€β”€ routes/                # Express route definitions
β”‚   β”œβ”€β”€ utils/                 # Utility functions (e.g., token generation)
β”‚   └── server.js              # Express server entry point
β”œβ”€β”€ .dockerignore              # Files to ignore in Docker build
β”œβ”€β”€ .env                       # Environment variables for the backend
β”œβ”€β”€ .gitignore                 # Git ignored files
β”œβ”€β”€ Dockerfile                 # Docker configuration for full app deployment
β”œβ”€β”€ LICENSE
β”œβ”€β”€ package.json               # Backend dependencies and scripts
β”œβ”€β”€ package-lock.json
└── README.md                  # Project documentation

βš™οΈ Setup & Installation

Follow the steps below to set up and run the project successfully:

1. βœ… Clone the Repository:

git clone https://github.com/PAIshanMadusha/advanced-mern-auth-system.git

2. βœ… Navigate to the Project Directory:

cd advanced-mern-auth-system

3. βœ… Create the .env File:

Create a .env file in the root directory of the project using the structure below:

MONGODB_URL=your_mongo_db_connection_string
PORT=5000
JWT_SECRET=your_jwt_secret
CLIENT_URL=http://localhost:5000
NODE_ENV=production
MAILTRAP_TOKEN=your_mailtrap_token
MAILTRAP_ENDPOINT=https://send.api.mailtrap.io/
BREVO_API_KEY=your_brevo_api_key
BREVO_SENDER_EMAIL=your_sender_email
BREVO_SENDER_NAME=your_sender_name

πŸ” Environment Variable Details:

MONGODB_URL

* Your MongoDB connection string. Create a free cluster using MongoDB Atlas, then copy the connection URI and paste it here.

PORT

* The port on which your backend server will run. Default is 5000.

JWT_SECRET

* A secret key used to sign and verify JWT tokens. You can generate a secure random value by running the following command in your terminal:

node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"

CLIENT_URL

* The base URL of your frontend application: In development: http://localhost:5173, In production or Docker setups: http://localhost:5000

NODE_ENV

* Defines the environment mode. Use: development during local development and production when deploying to production

MAILTRAP_TOKEN

* API token used to send test emails via Mailtrap: Log in or sign up at Mailtrap.io, then navigate to: Email API β†’ Demo Inbox β†’ Integrations** to find your token.

MAILTRAP_ENDPOINT

* API endpoint used by Mailtrap to send emails. Use the following value:

https://send.api.mailtrap.io/

BREVO_API_KEY

* Your Brevo (formerly Sendinblue) API key for sending real production emails, and generate it from SMTP & API under your Brevo Dashboard.

BREVO_SENDER_EMAIL

* A verified sender email used to send emails via Brevo in production, and set this from Senders, Domains & Dedicated IPs β†’ Add Sender in your Brevo account.

BREVO_SENDER_NAME

* The name that appears in the "From Name" field when sending emails, and this can be your app name, personal name, or business name. Configure it along with the sender email in Brevo.


4. βœ… Build & Run the App in Production:

* Install dependencies, build frontend and backend:

npm run build

* Start the application:

npm start

5. ⚑ Successful Startup Output:

* If everything is configured properly, your terminal will show:

Server is running on port: 5000
MongoDB Connected: your-cluster.mongodb.net

6. 🌐 Test the App:

* Visit: http://localhost:5000, and test the app in production mode using your registered credentials.


βš™οΈ Development Mode (Optional)

To run the project in development mode:

1. βœ… Edit your .env file and set the following values:

NODE_ENV=development
CLIENT_URL=http://localhost:5173

* Also, make sure to accurately set the Mailtrap keys (MAILTRAP_TOKEN, MAILTRAP_ENDPOINT) in the same .env file.

2. βœ… Start the frontend and backend development servers:

* Open two terminals and navigate to the following folders:

cd advanced-mern-auth-system/server
npm run dev
cd advanced-mern-auth-system/client
npm run dev

3. ⚑ Successful Startup Output:

* In the client terminal, you should see:

  http://localhost:5173/

* In the server terminal, you should see something like:

Server is running on port: 5000
MongoDB Connected: your-cluster.mongodb.net

4. 🌐 Test the App:

* Visit: http://localhost:5173, and test the app in development mode using your registered credentials.


🐳 Dockerized Deployment (Optional)

This application is fully containerized using Docker with multi-stage builds for both the client and server. It can be deployed on DigitalOcean or any platform that supports Docker containers, using GitHub integration and environment-specific .env secrets for production.

1. βœ… Ensure Docker Desktop is installed and running:

* If not installed, download it from Docker Official Site and, docker must be configured to use Linux containers.

2. βœ… Build the Docker Image:

* From the project root directory, run the following command to build the Docker image:

docker build -t advanced-mern-auth .

3. βœ… Run the Docker Container:

* After a successful build, run the Docker container with your .env configuration:

docker run --env-file .env -p 5000:5000 advanced-mern-auth

4. ⚑ Terminal output should confirm:

* You should see your frontend and backend running successfully from the container:

Server is running on port: 5000
MongoDB Connected: your-cluster.mongodb.net

5. 🌐 Verify It's Running:

* Open your browser and go to: πŸ‘‰ http://localhost:5000

πŸ“Έ System Screenshots:

These screenshots illustrate how the system appears on desktop:


Screenshot 1

Screenshot 3

Screenshot 4

Screenshot 4


πŸ‘¨β€πŸ’» Created by:

Ishan Madhusha
GitHub: PAIshanMadusha

Feel free to explore my work and get in touch if you'd like to collaborate! πŸš€


πŸ“ License:

This project is licensed under the MIT License : See the LICENSE file for details.

About

Docker-containerized MERN (MongoDB, Express, React, Node.js) authentication system with Mailtrap and Brevo integration for email delivery. It supports secure user registration, email verification, password recovery, and session management using modern technologies and cloud services.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published