Skip to content

melaku3/todo-list-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“Œ Todo List API

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.

πŸš€ Features

  • 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.

βš™οΈ Tech Stack

  • Backend: Node.js, Express.js
  • Database: MongoDB (Mongoose)
  • Authentication: JWT (jsonwebtoken) & bcrypt for password hashing
  • Validation: Zod
  • Error Handling: Centralized middleware

πŸ“‚ Project Structure

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  

πŸ› οΈ Installation & Setup

1️⃣ Clone the repository

git clone https://github.com/melaku3/todo-list-api.git
cd todo-api

2️⃣ Install dependencies

npm install

3️⃣ Set up environment variables

Create a .env file in the root directory and configure:

PORT=3000
MONGO_URI=your_mongodb_connection_string
JWT_SECRET=your_secret_key

4️⃣ Start the server

npm start

The API will run at http://localhost:3000.


πŸ“Œ API Documentation

πŸ” User Authentication

1️⃣ Register a New User

POST /api/auth/register

Request 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"
    }
}

2️⃣ User Login

POST /api/auth/login

Request Body (JSON):

{
    "email": "johndoe@example.com",
    "password": "securepassword"
}

Response:

{
    "message": "User logged in successfully"
}

πŸ“ Todo List Management

3️⃣ Create a New To-Do

POST /api/todos

Headers:

Authorization: Bearer your_jwt_token

Request Body (JSON):

{
    "title": "Buy groceries",
    "description": "Milk, eggs, bread",
    "status": true
}

Response:

{
    "message": "Todo created successfully"
}

4️⃣ Get All To-Dos

GET /api/todos

Headers:

Authorization: Bearer your_jwt_token

Response:

[
    {
        "_id": "651234abcd",
        "title": "Buy groceries",
        "description": "Milk, eggs, bread",
        "status": false,
        "userId": "651234abcd"
    }
]

5️⃣ Update a To-Do

PATCH /api/todos/:id

Headers:

Authorization: Bearer your_jwt_token

Request Body (JSON):

{
    "status": true
}

Response:

{
    "message": "Todo updated successfully"
}

6️⃣ Delete a To-Do

DELETE /api/todos/:id

Headers:

Authorization: Bearer your_jwt_token

Response:

{
    "message": "Todo deleted successfully"
}

7️⃣ Filter To-Dos by Status

GET /api/todos/status/:status

Headers:

Authorization: Bearer your_jwt_token

Response:

[
    {
        "_id": "651234abcd",
        "title": "Buy groceries",
        "description": "Milk, eggs, bread",
        "status": true,
        "userId": "651234abcd"
    }
]

πŸ”’ Authentication & Security

  • JWT Authentication: Users must include a valid JWT token in the Authorization header 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 Handling

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"

πŸ“ž Contact

For any issues, feel free to reach out! πŸš€
Email: emelaku63@gmail.com

About

Build a RESTful API to allow users to manage their to-do list.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors