A Node.js/Express backend for authentication, user management, and email verification, featuring JWT-based authentication, password reset, and email notifications via Mailtrap.
- User registration with email verification
- JWT authentication (login/logout)
- Password reset via email
- Welcome and notification emails (Mailtrap)
- MongoDB (Mongoose) for data storage
- Secure cookies for session management
- Node.js
- Express.js
- MongoDB & Mongoose
- JWT (jsonwebtoken)
- bcryptjs (password hashing)
- Mailtrap (email service)
- dotenv (environment variables)
- cookie-parser, cors
- Node.js (v18+ recommended)
- MongoDB instance (local or cloud)
- Mailtrap account (for email testing)
- Clone the repository:
git clone <repo-url> cd auth
- Install dependencies:
npm install
- Create a
.envfile in the root directory and add the following:PORT=5000 MONGO_URI=<your-mongodb-uri> JWT_SECRET=<your-jwt-secret> MAILTRAP_ENDPOINT=<your-mailtrap-endpoint> MAILTRAP_TOKEN=<your-mailtrap-token> CLIENT_URL=http://localhost:5173 NODE_ENV=development
- Development mode (with nodemon):
npm run dev
- Production mode:
npm start
POST /api/auth/signup— Register a new userPOST /api/auth/login— LoginPOST /api/auth/logout— LogoutPOST /api/auth/verify-email— Verify email with codePOST /api/auth/forgot-password— Request password resetPOST /api/auth/reset-password/:token— Reset passwordGET /api/auth/check-auth— Check authentication (requires token)
backend/
controllers/ # Route handlers (auth logic)
db/ # Database connection
mailtrap/ # Email templates & Mailtrap config
middleware/ # JWT verification middleware
models/ # Mongoose models
routes/ # Express routes
utils/ # Utility functions (JWT, cookies)
index.js # Main server entry point
MIT