An open API service indexing awesome lists of open source software.

https://github.com/yuvrajnode/todo-backend-with-database

A secure Node.js API for user authentication and todo management using Express, MongoDB, and JWT.
https://github.com/yuvrajnode/todo-backend-with-database

express jwt-authentication mongodb nodejs

Last synced: about 1 month ago
JSON representation

A secure Node.js API for user authentication and todo management using Express, MongoDB, and JWT.

Awesome Lists containing this project

README

          

# ๐Ÿง  Auth Todo API

A simple RESTful backend API built with **Node.js**, **Express**, **MongoDB**, and **JWT** that allows users to register, log in, and manage their personal todo tasks.

---

## ๐Ÿš€ Features

- โœ… User Signup and Login
- ๐Ÿ” JWT-based Authentication using custom `token` header
- ๐Ÿ“ Create, fetch, update, and delete todos
- ๐Ÿ—ƒ๏ธ MongoDB with Mongoose schemas & models
- ๐Ÿ“ฆ Clean JSON-based REST API

---

## ๐Ÿ“ Project Structure

```
โ”œโ”€โ”€ index.js # Main server file (routes, middleware, logic)
โ”œโ”€โ”€ db.js # MongoDB schemas and models
โ”œโ”€โ”€ package.json # Project metadata and dependencies
โ”œโ”€โ”€ .gitignore # Files to ignore in version control
```

---

## ๐Ÿ› ๏ธ Tech Stack

- **Node.js**
- **Express.js**
- **MongoDB** with **Mongoose**
- **JWT** for authentication
- **bcrypt** for password hashing
- **zod** for request validation

---

## ๐Ÿงช API Endpoints

### ๐Ÿ”น POST `/signup`
Registers a new user.

**Request Body:**
```json
{
"email": "user@example.com",
"password": "yourpassword",
"name": "Yuvraj"
}
```

**Response:**
```json
{
"message": "You are signed up"
}
```

---

### ๐Ÿ”น POST `/signin`
Authenticates the user and returns a JWT token.

**Request Body:**
```json
{
"email": "user@example.com",
"password": "yourpassword"
}
```

**Response:**
```json
{
"token": "JWT_TOKEN_HERE"
}
```

---

### ๐Ÿ”น POST `/todo`
Creates a new todo.

**Headers:**
```
token: JWT_TOKEN_HERE
```

**Request Body:**
```json
{
"title": "Learn Node.js"
}
```

**Response:**
```json
{
"message": "Todo created"
}
```

---

### ๐Ÿ”น POST `/todos`
Fetches all todos of the authenticated user.

**Headers:**
```
token: JWT_TOKEN_HERE
```

**Response:**
```json
{
"todos": [
{
"_id": "todoId",
"title": "Learn Node.js",
"done": false,
"userId": "userId"
}
]
}
```

---

### ๐Ÿ”น PUT `/todo/:id`
Updates a todo (e.g., mark as done).

**Headers:**
```
token: JWT_TOKEN_HERE
```

**Request Body:**
```json
{
"done": true
}
```

**Response:**
```json
{
"message": "Todo updated"
}
```

---

### ๐Ÿ”น DELETE `/todo/:id`
Deletes a todo.

**Headers:**
```
token: JWT_TOKEN_HERE
```

**Response:**
```json
{
"message": "Todo deleted"
}
```

---

## โš™๏ธ Setup Instructions

1. **Clone the repository:**
```bash
git clone https://github.com/yuvrajnode/todo-backend-with-database.git
cd todo-backend-with-database
```

2. **Install dependencies:**
```bash
npm install
```

3. **Configure MongoDB URI and secret (optional):**
If using environment variables, create a `.env` file and add:
```
MONGO_URI=your_mongo_connection_string
JWT_SECRET=your_secret
```
(Or keep them hardcoded in `index.js` as done currently.)

4. **Start the server:**
```bash
node index.js
```

Server will run at:
๐Ÿ“ `http://localhost:3000`

---

## ๐Ÿ™‹โ€โ™‚๏ธ Author

Made with โค๏ธ by **Yuvraj Singh**
GitHub: [@yuvrajnode](https://github.com/yuvrajnode)

---

## ๐Ÿ“œ License

This project is open source and free to use under the [MIT License](LICENSE).