A full-stack authentication system built with the MERN stack that implements secure user authentication using JWT, HttpOnly cookies, refresh tokens, and protected routes.
This project demonstrates modern authentication practices used in real-world web applications, including secure password hashing, session management, and automatic authentication persistence.
- User Registration
- User Login
- User Logout
- Get Current User Profile
- Protected Routes
- Password Hashing with bcrypt
- JWT Authentication
- Access & Refresh Token Flow
- HttpOnly Cookie Authentication
- Refresh Token API
- Authentication Middleware
- MongoDB Integration
- React + Vite
- Tailwind CSS
- React Router
- Protected Routes
- Public Route Guards
- Global Authentication Context
- Automatic Session Restoration
- Responsive UI
- Custom 404 Page
- React
- Vite
- React Router DOM
- Tailwind CSS
- Axios
- React Icons
- React Hot Toast
- Node.js
- Express.js
- MongoDB
- Mongoose
- JWT (jsonwebtoken)
- bcrypt
- cookie-parser
- cors
- dotenv
authentication-system/
│
├── client/
│ ├── src/
│ │ ├── components/
│ │ ├── context/
│ │ ├── pages/
│ │ ├── routes/
│ │ ├── services/
│ │ └── App.jsx
│ │
│ └── package.json
│
├── server/
│ ├── config/
│ ├── controllers/
│ ├── middleware/
│ ├── models/
│ ├── routes/
│ ├── utils/
│ ├── app.js
│ └── server.js
│
└── README.md
Register
│
▼
Password Hashing (bcrypt)
│
▼
Store User in MongoDB
│
▼
Login
│
▼
Generate Access Token
Generate Refresh Token
│
▼
Store Tokens in HttpOnly Cookies
│
▼
Access Protected Routes
│
▼
Authentication Middleware
│
▼
Verify JWT
│
▼
Return Protected Data
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/auth/register |
Register a new user |
| POST | /api/auth/login |
Login user |
| POST | /api/auth/logout |
Logout user |
| GET | /api/auth/profile |
Get logged-in user |
| POST | /api/auth/refresh-token |
Generate new access token |
PORT=5000
MONGODB_URI=your_mongodb_connection_string
ACCESS_TOKEN_SECRET=your_access_secret
REFRESH_TOKEN_SECRET=your_refresh_secret
ACCESS_TOKEN_EXPIRY=15m
REFRESH_TOKEN_EXPIRY=7d
CLIENT_URL=http://localhost:5173
NODE_ENV=developmentgit clone https://github.com/jdcodebase/authentication-systemcd authentication-system
cd serverInstall dependencies
npm installStart development server
npm run devcd clientInstall dependencies
npm installStart development server
npm run devThis project uses:
- JWT Access Tokens
- JWT Refresh Tokens
- HttpOnly Cookies
- Secure Cookie Configuration
- Automatic Token Refresh
- Session Restoration on Page Reload
- Password hashing with bcrypt
- HttpOnly cookies
- JWT authentication
- Access & refresh token separation
- Protected API routes
- Authentication middleware
- Secure CORS configuration
- Environment variable management
This project helped me understand:
- JWT Authentication
- Access & Refresh Tokens
- Password Hashing
- HttpOnly Cookies
- Authentication Middleware
- Protected Routes
- React Context API
- React Router Guards
- Session Persistence
- Full-stack Authentication Flow
- Deploying MERN Applications
Frontend: Vercel
Backend: Render
Database: MongoDB Atlas
Jatin Dhamija
GitHub: https://github.com/jdcodebase
LinkedIn: https://linkedin.com/in/jatindhamija