Folders and files Β
Β
Β
Β
Β
Β
View all files
Repository files navigation # SecureChat ππ¬
A production-ready **MERN stack real-time chat application** with private 2-person rooms, media sharing, JWT security, admin panel, and 24-hour auto-deletion.
---
## β¨ Features
| Feature | Details |
|---|---|
| **Room Code System** | Any 2 users sharing the same code get paired instantly |
| **Real-time Messaging** | Socket.io for live messages, typing, and online indicators |
| **Media Uploads** | Images from gallery or camera β uploaded to Cloudinary |
| **24h Auto-delete** | All rooms, messages, and media auto-expire via cron job |
| **JWT Auth** | httpOnly cookies + localStorage, rate-limited auth endpoints |
| **Admin Panel** | Full dashboard at `/admin` with charts, room/media/user management |
| **Mobile Responsive** | Works on all screen sizes |
---
## ποΈ Project Structure
```
chat-app/
βββ client/ # Vite + React frontend (port 5173)
β βββ src/
β βββ pages/ Home, Chat, AdminLogin, AdminDashboard
β βββ components/ ChatBox, MessageBubble, MediaUpload, TypingIndicator
β β βββ admin/ AdminTable, StatsCard, Charts
β βββ context/ AuthContext, SocketContext
β βββ utils/ api.js (Axios)
β
βββ server/ # Express + Node.js backend (port 5000)
βββ controllers/ auth, chat, room, media, admin
βββ models/ User, Room, Message, Media
βββ routes/ authRoutes, chatRoutes, roomRoutes, adminRoutes
βββ middleware/ authMiddleware, adminMiddleware, uploadMiddleware
βββ utils/ cloudinary.js, cronJobs.js
βββ socket/ socketHandler.js
```
---
## π Quick Start
### Prerequisites
- **Node.js** v18+
- **MongoDB** (Atlas or local)
- **Cloudinary** account (free tier works)
---
### 1. Clone & Install
```bash
# Backend
cd server
npm install
# Frontend
cd ../client
npm install
```
### 2. Configure Environment Variables
Edit `server/.env`:
```env
PORT=5000
MONGODB_URI=mongodb://localhost:27017/chatapp
# OR for Atlas: mongodb+srv://user:pass@cluster.mongodb.net/chatapp
JWT_SECRET=your_super_secret_jwt_key_min_32_chars
JWT_EXPIRES_IN=7d
ADMIN_USERNAME=admin
ADMIN_PASSWORD=YourSecureAdminPassword
ADMIN_JWT_SECRET=your_admin_jwt_secret_key
CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret
CLIENT_URL=http://localhost:5173
NODE_ENV=development
```
### 3. Run Development Servers
Open two terminals:
```bash
# Terminal 1 β Backend
cd server
npm run dev
# Server starts at http://localhost:5000
# Terminal 2 β Frontend
cd client
npm run dev
# App opens at http://localhost:5173
```
---
## π Authentication
- **Users**: Register/login at `http://localhost:5173`
- **Admins**: Login at `http://localhost:5173/admin/login`
- Default credentials from `.env`: `ADMIN_USERNAME` / `ADMIN_PASSWORD`
---
## π¬ How to Chat
1. Open `http://localhost:5173` in **two different browser windows** (or two different browsers)
2. **Register/login** in both windows with different usernames
3. Enter the **same room code** (e.g. `hello`) in both windows
4. Start chatting in real time! π
---
## π οΈ Admin Panel
Visit `http://localhost:5173/admin/login` and log in with your admin credentials.
**Features:**
- π Live stats (active rooms, messages today, media uploads, total users)
- π Charts: messages per hour + per day (Recharts)
- π Room management (view, delete)
- πΌοΈ Media management (view, delete from Cloudinary)
- π€ User management (ban/unban, IP-based banning)
---
## ποΈ Auto-Deletion
A **node-cron** job runs every hour and deletes:
- Rooms older than 24 hours
- Messages older than 24 hours
- Media files from both MongoDB and Cloudinary
MongoDB also has **TTL indexes** on all collections as a backup.
---
## π Security
- JWT tokens stored in **httpOnly cookies** (XSS-resistant)
- **Rate limiting** on all API routes (stricter on auth endpoints)
- **Helmet.js** security headers
- **Image-only** file upload validation (MIME + extension check, 5MB limit)
- Room messages are strictly **scoped to room members**
- Admin uses a **separate JWT secret** and cookie
---
## π‘ API Endpoints
### Auth
| Method | Route | Description |
|---|---|---|
| POST | `/api/auth/register` | Register new user |
| POST | `/api/auth/login` | Login |
| POST | `/api/auth/logout` | Logout |
| GET | `/api/auth/me` | Get current user |
### Rooms (Protected)
| Method | Route | Description |
|---|---|---|
| POST | `/api/rooms/join` | Join/create room by code |
| GET | `/api/rooms/:code` | Get room info |
| POST | `/api/rooms/leave` | Leave room |
### Chat (Protected)
| Method | Route | Description |
|---|---|---|
| GET | `/api/chat/:roomId/messages` | Get messages (paginated) |
| POST | `/api/chat/:roomId/message` | Send text message |
| POST | `/api/chat/media/upload` | Upload image |
### Admin (Admin JWT required)
| Method | Route | Description |
|---|---|---|
| POST | `/api/admin/login` | Admin login |
| GET | `/api/admin/stats` | Dashboard stats + chart data |
| GET | `/api/admin/rooms` | List all rooms |
| DELETE | `/api/admin/rooms/:id` | Delete room |
| GET | `/api/admin/media` | List all media |
| DELETE | `/api/admin/media/:id` | Delete media |
| GET | `/api/admin/users` | List all users |
| POST | `/api/admin/users/:id/ban` | Ban user |
| POST | `/api/admin/users/:id/unban` | Unban user |
---
## π Socket.io Events
| Event | Direction | Description |
|---|---|---|
| `join-room` | Client β Server | Join a chat room |
| `send-message` | Client β Server | Send a message |
| `typing` | Client β Server | Start typing indicator |
| `stop-typing` | Client β Server | Stop typing indicator |
| `new-message` | Server β Client | New message received |
| `user-joined` | Server β Client | Partner joined room |
| `user-offline` | Server β Client | Partner disconnected |
| `user-typing` | Server β Client | Partner is typing |
| `user-stop-typing` | Server β Client | Partner stopped typing |
---
## ποΈ Production Deployment
```bash
# Build frontend
cd client
npm run build
# Output in client/dist/
# Serve dist/ with your server or a CDN
# Set NODE_ENV=production in server/.env
# Use a process manager like PM2:
npm install -g pm2
cd server
pm2 start index.js --name securechat
```
---
## π¦ Tech Stack
| Layer | Technology |
|---|---|
| Frontend | React 18, Vite, Tailwind CSS v3, Recharts, Socket.io-client |
| Backend | Node.js, Express.js, Socket.io |
| Database | MongoDB, Mongoose |
| Auth | JWT (jsonwebtoken, bcryptjs) |
| Media | Cloudinary, Multer |
| Security | Helmet, express-rate-limit |
| Scheduling | node-cron |
---
## π License
MIT β Free to use, modify, and distribute.
# chat-app
You canβt perform that action at this time.