Skip to content

πŸ” Full-stack MERN authentication system with email verification, OTP-based password reset, and secure JWT login/logout using cookies.

Notifications You must be signed in to change notification settings

soumojit-D48/MernAuth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

14 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

MERN Auth System πŸ”

πŸš€ Live Demo

πŸ”— View it on Render

A complete full-stack MERN (MongoDB, Express, React, Node.js) authentication system with:

  • βœ… User registration & login
  • βœ… JWT authentication via HTTP-only cookies
  • βœ… Email verification using OTP
  • βœ… Password reset via OTP
  • βœ… Protected routes using middleware
  • βœ… Nodemailer integration

πŸ”§ Tech Stack

πŸ“¦ Backend

  • Node.js
  • Express
  • MongoDB with Mongoose
  • JWT (jsonwebtoken)
  • Bcrypt
  • Nodemailer
  • dotenv

🎨 Frontend

  • React (Vite)
  • Axios
  • React Router DOM
  • Tailwind CSS
  • React Toastify

πŸ—‚οΈ Folder Structure

.
β”œβ”€β”€ client               # React Frontend (Vite)
β”‚   β”œβ”€β”€ public
β”‚   └── src
β”‚       β”œβ”€β”€ assets       # Images/icons
β”‚       β”œβ”€β”€ components   # Reusable UI components (Navbar, Header)
β”‚       β”œβ”€β”€ context      # AppContext for global state
β”‚       └── pages        # Route pages (Login, Register, Verify, Reset)
β”œβ”€β”€ server               # Node.js Backend
β”‚   β”œβ”€β”€ config           # MongoDB config
β”‚   β”œβ”€β”€ controllers      # Route controller logic
β”‚   β”œβ”€β”€ middlewares      # JWT middleware (userAuth)
β”‚   β”œβ”€β”€ models           # Mongoose models
β”‚   └── routes           # Express routers

πŸ§ͺ Features

  1. πŸ” Registration & Login Secure password hashing with bcrypt

JWT token stored in HTTP-only cookies

Persistent login on refresh

  1. πŸ“§ Email Verification After register, user receives 6-digit OTP

User must verify their account

OTP expires after 24 hours

  1. πŸ” Password Reset User enters email to get OTP

OTP expires in 15 mins

After verifying OTP, user can reset password

  1. πŸ›‘οΈ Protected Routes Custom middleware (userAuth) to protect routes using token in cookies

πŸ”„ API Routes

Auth

  • POST /api/auth/register β†’ Register a new user
  • POST /api/auth/login β†’ Login existing user
  • POST /api/auth/logout β†’ Logout the current user
  • POST /api/auth/send-verify-otp β†’ Send email verification OTP
  • POST /api/auth/verify-account β†’ Verify user email using OTP
  • GET /api/auth/is-auth β†’ Check if user is authenticated
  • POST /api/auth/send-reset-otp β†’ Send password reset OTP to email
  • POST /api/auth/reset-password β†’ Reset password using OTP

User

  • GET /api/user/data β†’ Fetch authenticated user's data

🧠 Learning Outcomes

Full-stack authentication using JWT and cookies

Building secure REST APIs with Express

Working with protected routes and middleware

Managing global app state using React Context API

✨ Credits

Built using React, Node.js, Express, MongoDB

OTP via Nodemailer

Auth with JWT

About

πŸ” Full-stack MERN authentication system with email verification, OTP-based password reset, and secure JWT login/logout using cookies.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published