A secure and modern MERN Stack (MongoDB, Express, React, Node.js) based Authentication System with JWT tokens, cookie-based sessions, and clean cross-origin (CORS) handling.
🔐 Built with real-world use cases in mind, this project ensures login persistence, protected routes, forgot password via email, and production-ready configuration for frontend-backend deployment across different domains.
- ✅ JWT authentication with
HttpOnly
secure cookies - ✅ Cross-domain support with
CORS
andcredentials
- ✅ Auto login session persistence on page reload
- ✅ Role-based / account-verified based routing
- ✅ Forgot password via email (reset token + expiration)
- ✅ Error handling with client-friendly messages
- ✅ React context for auth & user state management
- ✅ Toast notifications for feedback
- ✅ Production-ready
secure
,sameSite
, andpath
cookie configs
While most auth systems either:
- Store tokens insecurely in localStorage/sessionStorage, or
- Work only on the same origin,
This system uses:
HttpOnly
secure cookies for better security (no XSS token theft)withCredentials
&sameSite: "none"
for full cross-origin session support- Frontend/Backend deployed on separate domains (ideal for real-world apps)
- Password reset functionality with secure email token-based workflow
- Frontend: React, Axios, React Router, Toastify
- Backend: Node.js, Express, MongoDB, JWT, Cookie-parser, Nodemailer
- Database: MongoDB Atlas
- Deployment: Render (frontend) & Render (backend)
git clone https://github.com/rajasneh/MERN-Authentication-System
cd server
npm install
Create a .env
file in the /server
directory:
MONGO_URI=your_mongo_uri
JWT_SECRET=your_jwt_secret
NODE_ENV=development
SMTP_USER=your SMTP user
SMTP_PASS=your SMTP password
SENDER_EMAIL=Email of sender
Start backend:
npm start server
cd ../client
npm install
Create a .env
file in the /client
directory:
VITE_BACKEND_URL=http://localhost:4000
Start frontend:
npm run dev
POST /api/auth/register
→ Register a new userPOST /api/auth/login
→ Login with email & passwordPOST /api/auth/logout
→ Logout and clear cookiePOST /api/auth/send-verify-otp
→ Send email verify OTP to emailGET /api/auth/verify-account
→ Checks the email verify otp and verifiesGET /api/auth/is-auth
→ Check if user is authenticatedPOST /api/auth/send-reset-otp
→ Send password reset OTP to email-
POST /api/auth/send-reset-otp
→ Send OTP to reset password
-
POST /api/auth/reset-password
→ Verifies the password reset OTP and give option to reset the password
GET /api/user/data
→ Get user details if authenticated
- Visit:
http://localhost:5173
- Register or login
- Upon login, JWT is stored in a secure cookie (HttpOnly)
- Auto-login is maintained via cookies
- Logout manually to clear the session
- Use "Forgot Password" to reset via email token
- Set
NODE_ENV=production
in backend.env
- Ensure cookie config uses
secure: true
andsameSite: "none"
for cross-origin - Configure CORS properly on both Vercel and Render:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: <client_url>
Asneh Raj – rajasneh91@gmail.com