A RESTful API built with Node.js, Express.js, and MongoDB that allows users to register, log in, and manage their to-do lists securely.
- User Authentication: Register and login with JWT-based authentication.
- CRUD Operations: Create, Read, Update, and Delete to-do items.
- Filtering: Fetch to-do items based on their status (true for completed/false for pending).
- Security: Passwords are securely hashed, and users can access only their own data.
- Backend: Node.js, Express.js
- Database: MongoDB (Mongoose)
- Authentication: JWT (jsonwebtoken) & bcrypt for password hashing
- Validation: Zod
- Error Handling: Centralized middleware
todo-api/
βββ src/
β βββ controllers/ # Request handling logic
β βββ models/ # Mongoose schemas
β βββ routes/ # API route handlers
β βββ middlewares/ # Authentication & validation
β βββ utils/ # Utility functions
β βββ config/ # Database & environment config
β βββ app.js # Express app setup
β βββ server.js # Main entry point
βββ .env # Environment variables
βββ package.json
βββ README.md
git clone https://github.com/melaku3/todo-list-api.git
cd todo-apinpm installCreate a .env file in the root directory and configure:
PORT=3000
MONGO_URI=your_mongodb_connection_string
JWT_SECRET=your_secret_key
npm startThe API will run at http://localhost:3000.
POST /api/auth/registerRequest Body (JSON):
{
"name": "John Doe",
"email": "johndoe@example.com",
"password": "securepassword"
}Response:
{
"message": "User created successfully",
"user": {
"_id": "651234abcd",
"name": "John Doe",
"email": "johndoe@example.com"
}
}POST /api/auth/loginRequest Body (JSON):
{
"email": "johndoe@example.com",
"password": "securepassword"
}Response:
{
"message": "User logged in successfully"
}POST /api/todosHeaders:
Authorization: Bearer your_jwt_token
Request Body (JSON):
{
"title": "Buy groceries",
"description": "Milk, eggs, bread",
"status": true
}Response:
{
"message": "Todo created successfully"
}GET /api/todosHeaders:
Authorization: Bearer your_jwt_token
Response:
[
{
"_id": "651234abcd",
"title": "Buy groceries",
"description": "Milk, eggs, bread",
"status": false,
"userId": "651234abcd"
}
]PATCH /api/todos/:idHeaders:
Authorization: Bearer your_jwt_token
Request Body (JSON):
{
"status": true
}Response:
{
"message": "Todo updated successfully"
}DELETE /api/todos/:idHeaders:
Authorization: Bearer your_jwt_token
Response:
{
"message": "Todo deleted successfully"
}GET /api/todos/status/:statusHeaders:
Authorization: Bearer your_jwt_token
Response:
[
{
"_id": "651234abcd",
"title": "Buy groceries",
"description": "Milk, eggs, bread",
"status": true,
"userId": "651234abcd"
}
]- JWT Authentication: Users must include a valid JWT token in the
Authorizationheader to access protected routes. The token is stored as an HTTP-only cookie, so it is automatically included in requests without manual intervention. - Password Hashing: Uses bcrypt to securely hash passwords.
- Access Control: Users can only manage their own to-dos.
| Error Type | Response Code | Example Message |
|---|---|---|
| Invalid Credentials | 401 | "Invalid email or password" |
| Unauthorized Access | 403 | "Access denied" |
| Resource Not Found | 404 | "To-do not found" |
| Validation Error | 400 | "Field is required" |
| Server Error | 500 | "Internal server error" |
For any issues, feel free to reach out! π
Email: emelaku63@gmail.com