A complete backend authentication system supporting User and Admin roles, featuring email verification, JWT-based authentication, protected routes, and password reset via email.
- User Signup
- Email Verification (via token)
- Login with JWT
- Protected Route (
/profile) - Forgot Password (email-based)
- Reset Password
- Admin Signup
- Email Verification
- Login with JWT
- Protected Route (
/profile) - Forgot Password
- Reset Password
- Password hashing using bcrypt
- JWT authentication & authorization
- Email verification tokens
- Password reset tokens
- Middleware-based route protection
- Environment variable configuration
- Backend: Node.js, Express.js
- Database: MongoDB (Mongoose)
- Authentication: JWT (jsonwebtoken)
- Password Hashing: bcrypt
- Email Service: Custom email service (Nodemailer or similar)
project-root
│
├── routes
│ ├── admin.js
│ └── user.js
│
├── middleware
│ ├── adminMiddleware.js
│ └── userMiddleware.js
│
├── services
│ └── emailService.js
│
├── db
│ └── index.js
│
├── .env
├── index.js
└── package.json
Create a .env file in the root directory:
PORT=3000
MONGO_URI=your_mongodb_connection_string
JWT_SECRET=admin_jwt_secret
USER_JWT_SECRET=user_jwt_secret
| Method | Endpoint | Description |
|---|---|---|
| POST | /user/signup |
Register a new user |
| GET | /user/verify-email?token= |
Verify email |
| POST | /user/login |
Login user |
| GET | /user/profile |
Protected route |
| POST | /user/forgot-password |
Send reset link |
| POST | /user/reset-password?token= |
Reset password |
| Method | Endpoint | Description |
|---|---|---|
| POST | /admin/signup |
Register admin |
| GET | /admin/verify-email?token= |
Verify email |
| POST | /admin/login |
Login admin |
| GET | /admin/profile |
Protected route |
| POST | /admin/forgot-password |
Send reset link |
| POST | /admin/reset-password?token= |
Reset password |
- User/Admin registers
- Password is hashed using bcrypt
- Verification token generated
- Email sent with verification link
- User/Admin logs in
- Password compared using bcrypt
- JWT token generated
- Token used for protected routes
- User requests reset link
- Token generated and emailed
- User submits new password
- Password updated after verification
- Verifies JWT from headers
- Attaches decoded user to
req.user
- Verifies JWT
- Attaches decoded admin to
req.admin
Use tools like:
- Postman
- Thunder Client
Example:
POST /user/login
Body:
{
"email": "test@example.com",
"password": "123456"
}- Add reset token expiry
- Add rate limiting (prevent brute force attacks)
- Add input validation (Zod/Joi)
- Refactor duplicate logic (admin/user services)
- Add frontend UI for reset password
Aryan Raj
This is a production-style authentication backend covering:
- Secure login system
- Email-based verification
- Token-based authentication
- Password recovery