- 🟢 Node.js + Express
- 🍃 MongoDB + Mongoose
- 🔑 JWT for authentication
- 🔒 bcrypt for password hashing
- ✅ Zod for validation
- 🛡️ Rate limiting for security
- ⚛️ React 19 + Vite
- 🧭 React Router v7
- 📡 Axios for API calls
- 🎯 Context API for state management
- 🎨 Tailwind CSS
- User registration with validation
- Secure login with JWT access tokens
- Refresh token mechanism using HTTP-only cookies
- Protected routes
- Rate limiting on sensitive endpoints
- CORS configuration
- Token-based authentication
Auth/
├── BackendAuth/ # Express backend
│ ├── index.js # Main server file
│ ├── Users.js # User model
│ └── .env # Environment variables
└── FrontendAuth/ # React frontend
├── src/
│ ├── components/ # React components
│ ├── api/ # Axios configuration
│ └── AuthContext.jsx
└── vite.config.js
cd BackendAuth
npm installCreate .env file:
SECRET_KEY=your_secret_key
ACCESS_TOKEN_TTL=15m
REFRESH_TOKEN_TTL=7dStart MongoDB:
mongodRun server:
npm run devServer runs on http://localhost:4000
cd FrontendAuth
npm install
npm run devFrontend runs on http://localhost:5173
POST /users - Register new user
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123"
}POST /login - Login user
{
"email": "john@example.com",
"password": "password123"
}POST /auth/refresh - Refresh access token
POST /logout - Logout user
GET /users-me - Get current user (requires Bearer token)
- User registers with email and password
- Password is hashed using bcrypt before storage
- On login, server issues:
- Access token (15 min expiry) sent in response
- Refresh token (7 days) stored in HTTP-only cookie
- Frontend stores access token and includes it in requests
- When access token expires, refresh token generates new access token
- Protected routes verify access token before granting access
- Password hashing with bcrypt (salt rounds: 10)
- HTTP-only cookies for refresh tokens
- Rate limiting (5 requests/min on login and refresh)
- CORS configuration for cross-origin requests
- Token scope validation (access vs refresh)
- Zod schema validation for inputs
| Variable | Description | Default |
|---|---|---|
SECRET_KEY |
JWT signing secret | Required |
ACCESS_TOKEN_TTL |
Access token expiry | 15m |
REFRESH_TOKEN_TTL |
Refresh token expiry | 7d |
Backend with auto-reload:
npm run devFrontend with hot reload:
npm run dev