A secure, production-ready authentication system built using the MERN stack with JWT access & refresh tokens, cookie-based authentication, and full deployment on Render.
This project demonstrates real-world authentication best practices including token rotation, protected routes, and secure logout.
👉 https://secure-auth-frontend-6nfe.onrender.com
👉 https://secure-auth-backend-xsa9.onrender.com
- React (Vite)
- JavaScript
- Fetch API
- React Router
- LocalStorage (access token)
- Deployed on Render
- Node.js
- Express.js
- MongoDB Atlas
- Mongoose
- JWT (Access & Refresh Tokens)
- HTTP-only Cookies
- Deployed on Render
- ✅ User Registration
- ✅ User Login
- ✅ JWT Access Token
- ✅ JWT Refresh Token (stored in HTTP-only cookie)
- ✅ Token rotation
- ✅ Protected routes
- ✅ Secure logout
- ✅ CORS configured for production
- ✅ Environment-based configuration
- ✅ Production deployment (Frontend + Backend)
- User logs in with email & password
- Backend returns:
- Access Token → stored in
localStorage - Refresh Token → stored in HTTP-only cookie
- Access Token → stored in
- Access token is used for API requests
- When access token expires:
- Refresh token generates a new access token
- Logout clears:
- Access token
- Refresh token cookie
Base URL: https://secure-auth-backend-xsa9.onrender.com
POST /api/auth/register POST /api/auth/login POST /api/auth/refresh POST /api/auth/logout GET /api/auth/me
PORT=5000 MONGO_URI=your_mongodb_atlas_uri ACCESS_TOKEN_SECRET=your_access_token_secret REFRESH_TOKEN_SECRET=your_refresh_token_secret FRONTEND_URL=https://secure-auth-frontend-6nfe.onrender.com
VITE_API_BASE_URL=https://secure-auth-backend-xsa9.onrender.com
.env files to GitHub
cd backend
npm install
npm run dev
###Frontend
cd frontend
npm install
npm run dev
🚀 Deployment Guide (High Level)
Backend (Render)
Create Web Service
Root directory: backend
Start command: npm start
Add environment variables
Enable cookies & CORS
Frontend (Render)
Create Static Site
Root directory: frontend
Build command: npm run build
Publish directory: dist
Set VITE_API_BASE_URL
🚀 Deployment Guide (High Level)
Backend (Render)
Create Web Service
Root directory: backend
Start command: npm start
Add environment variables
Enable cookies & CORS
Frontend (Render)
Create Static Site
Root directory: frontend
Build command: npm run build
Publish directory: dist
Set VITE_API_BASE_URL
VITE_API_BASE_URL
🔒 Security Best Practices Used
HTTP-only cookies for refresh tokens
Short-lived access tokens
Token rotation
CORS origin restriction
Environment variable secrets
No sensitive data in client
Common Issues Solved
❌ CORS errors in production
❌ Cookies not sent cross-origin
❌ Token refresh infinite loops
❌ Render path & build errors
❌ Frontend/Backend URL mismatch
🧠 What I Learned
Real-world JWT authentication
Secure token storage strategies
Production deployment on Render
Debugging CORS & cookie issues
Full-stack integration
👨💻 Author
Gaurav Gawade
GitHub: https://github.com/Ga1233
⭐ If you like this project
Give it a ⭐ on GitHub and feel free to fork it!