A production-grade authentication and user management system built with the MERN stack (MongoDB, Express, React, Node.js). Features JWT-based authentication, role-based access control, and a professional UI.
| Home Page | Register | Login |
|---|---|---|
![]() |
![]() |
![]() |
| Dashboard | Admin Panel |
|---|---|
![]() |
![]() |
- User Registration with input validation
- User Login with JWT token generation
- Logout with session termination
- Input Validation (name, email format, password strength)
- View Profile - See your details and account info
- Edit Profile - Update name, description, and school
- Delete Account - Permanently remove your account
- Stats Overview - Total users, admin count, regular users
- User Management - View all registered users in a table
- Search Users - Filter by name, email, or school
- Delete Users - Remove any user (self-deletion protected)
- Role Protection - Regular users cannot access admin routes
- Password Hashing with bcrypt (10 salt rounds)
- JWT Authentication with HTTP-Only cookies
- Role-Based Access Control (User vs Admin)
- Route Protection - Unauthorized users redirected
- CORS Configuration - Only allows trusted origins
- Input Sanitization - Prevents malicious data
| Layer | Technology |
|---|---|
| Frontend | React.js (Vite), Tailwind CSS, React Router, Axios |
| Backend | Node.js, Express.js |
| Database | MongoDB (Mongoose ODM) |
| Authentication | JWT (jsonwebtoken), bcryptjs |
| Validation | express-validator |
| Deployment | Vercel (Frontend), Render (Backend), MongoDB Atlas (Database) |
authsphere/
βββ backend/
β βββ src/
β β βββ config/
β β β βββ db.js # MongoDB connection
β β βββ controllers/
β β β βββ authController.js # Register, Login, Logout
β β β βββ userController.js # Profile CRUD
β β β βββ adminController.js # Admin operations
β β βββ middleware/
β β β βββ authMiddleware.js # JWT verification
β β β βββ adminMiddleware.js # Admin role check
β β βββ models/
β β β βββ User.js # User schema
β β βββ routes/
β β β βββ authRoutes.js # Auth endpoints
β β β βββ userRoutes.js # User endpoints
β β β βββ adminRoutes.js # Admin endpoints
β β βββ utils/
β β βββ generateToken.js # JWT generation
β βββ .env # Environment variables
β βββ server.js # Entry point
β βββ package.json
βββ frontend/
βββ src/
β βββ components/
β β βββ Navbar.jsx # Navigation bar
β β βββ PrivateRoute.jsx # Auth guard
β β βββ AdminRoute.jsx # Admin guard
β βββ context/
β β βββ AuthContext.jsx # Global state
β βββ pages/
β β βββ Home.jsx # Landing page
β β βββ Login.jsx # Login form
β β βββ Register.jsx # Registration form
β β βββ Dashboard.jsx # User profile
β β βββ AdminPanel.jsx # Admin dashboard
β βββ utils/
β βββ axios.js # API client
βββ vercel.json # SPA routing
βββ package.json
- Node.js (v18 or higher)
- MongoDB Atlas account
git clone https://github.com/ahammedSumon/authsphere.git
cd authspherecd backend
npm installCreate a .env file in the backend folder:
PORT=5000
MONGO_URI=your_mongodb_connection_string
JWT_SECRET=your_secret_key
Run the backend:
npm run devBackend runs at: http://localhost:5000
cd frontend
npm installRun the frontend:
npm run devFrontend runs at: http://localhost:5173
After registering, manually change role from "user" to "admin" in MongoDB Atlas to access admin features.
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/auth/register |
Register new user |
| POST | /api/auth/login |
Login & get token |
| POST | /api/auth/logout |
Logout & clear token |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/users/profile |
Get own profile |
| PUT | /api/users/profile |
Update profile |
| DELETE | /api/users/profile |
Delete account |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/admin/users |
Get all users |
| DELETE | /api/admin/users/:id |
Delete a user |
| Feature | Implementation |
|---|---|
| Password Hashing | bcrypt with 10 salt rounds |
| JWT Tokens | Signed with secret, 7-day expiry |
| HTTP-Only Cookies | Prevents XSS attacks |
| CORS | Whitelisted origins only |
| Input Validation | express-validator on all endpoints |
| Role Protection | Middleware checks req.user.role |
| Self-Delete Protection | Admin cannot delete own account |
Problem: Cookies set by Render backend were stripped by Cloudflare proxy, preventing authentication on Vercel frontend.
Solution: Implemented token transmission via request body and Authorization header as fallback, with localStorage for token persistence.
Problem: Hard refresh on Vercel returned 404 errors for client-side routes.
Solution: Added vercel.json with rewrite rules to serve index.html for all routes.
- Email verification with Nodemailer
- Password reset flow
- Profile picture uploads
- Pagination for admin user list
- Rate limiting for API protection
- Refresh token rotation
- Unit & integration tests
- Docker containerization
- Building RESTful APIs with Express.js
- Implementing JWT authentication
- Role-based authorization middleware
- MongoDB schema design with Mongoose
- React Context API for global state
- Protected routes with React Router
- Professional UI with Tailwind CSS
- CORS and cookie security
- Deploying to Vercel and Render
- Debugging cross-domain issues
- Git version control best practices
Salauddin Ahammed Sumon
π§ Email: salauddin18@cse.pstu.ac.bd
This project is open source and available under the MIT License.
β Star this repo if you found it helpful!




