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

https://github.com/saketkothari/tshirt-store-backend-api

A robust backend API for managing an online t-shirt store, handling product listings, orders, and customer data.
https://github.com/saketkothari/tshirt-store-backend-api

api authentication bcryptjs cloudinary cookie-parser ejs expressjs file-upload jwt-token mongodb mongoose morgan-middleware node-js nodemailer nodemon stripe swagger

Last synced: 12 days ago
JSON representation

A robust backend API for managing an online t-shirt store, handling product listings, orders, and customer data.

Awesome Lists containing this project

README

          

# ๐Ÿ‘• T-Shirt Store Backend API

A production-ready RESTful API for an e-commerce t-shirt store built with Node.js, Express, and MongoDB.

[![Live API](https://img.shields.io/badge/API-Live-brightgreen)](https://tshirt-store.onrender.com/api-docs/)
[![Node.js](https://img.shields.io/badge/Node.js-18+-green)](https://nodejs.org/)
[![Express](https://img.shields.io/badge/Express-4.x-blue)](https://expressjs.com/)
[![MongoDB](https://img.shields.io/badge/MongoDB-8.x-green)](https://www.mongodb.com/)
[![License](https://img.shields.io/badge/License-ISC-yellow)](./LICENSE)


T-Shirt Store API

## ๐Ÿ“š API Documentation

Explore the complete API documentation with interactive testing:

๐Ÿ”— **[Live Swagger Documentation](https://tshirt-store.onrender.com/api-docs/)**

---

## โœจ Features

### ๐Ÿ” Authentication & Authorization

- **JWT-based authentication** with httpOnly cookies
- **Role-based access control** (User, Manager, Admin)
- Secure password hashing with bcrypt
- Password reset via email tokens

### ๐Ÿ‘ค User Management

- User registration with profile photo upload
- Login/Logout with secure token handling
- Forgot password & reset password flow
- User dashboard with profile updates
- Admin can manage all users

### ๐Ÿ›๏ธ Product Management

- Full CRUD operations for products
- Multiple product images via Cloudinary
- Product categories (shortsleeves, longsleeves, sweatshirt, hoodies)
- Product search, filtering & pagination
- Product reviews and ratings system

### ๐Ÿ“ฆ Order Management

- Create and track orders
- Order history for users
- Admin order management with status updates
- Detailed order information with shipping details

### ๐Ÿ’ณ Payment Integration

- **Stripe** payment processing
- **Razorpay** payment processing
- Secure payment intent creation

---

## ๐Ÿ› ๏ธ Tech Stack

| Category | Technology |
| ------------------ | ------------------------- |
| **Runtime** | Node.js |
| **Framework** | Express.js |
| **Database** | MongoDB with Mongoose ODM |
| **Authentication** | JWT (JSON Web Tokens) |
| **File Upload** | Cloudinary |
| **Payments** | Stripe, Razorpay |
| **Email** | Nodemailer |
| **Documentation** | Swagger/OpenAPI 3.0 |
| **Validation** | Validator.js |

---

## ๐Ÿš€ Getting Started

### Prerequisites

- Node.js 18+ installed
- MongoDB database (local or Atlas)
- Cloudinary account
- Stripe account (for payments)
- Razorpay account (for payments)

### Installation

1. **Clone the repository**

```bash
git clone https://github.com/SaketKothari/tshirt-store-backend-api.git
cd tshirt-store-backend-api
```

2. **Install dependencies**

```bash
npm install
```

3. **Configure environment variables**

Create a `.env` file in the root directory:

```env
# Server
PORT=4000

# Database
MONGODB_URL=mongodb://localhost:27017/tshirt-store

# JWT
JWT_SECRET=your-super-secret-jwt-key
JWT_EXPIRY=3d
COOKIE_TIME=3

# Cloudinary
CLOUDINARY_NAME=your-cloud-name
CLOUDINARY_API_KEY=your-api-key
CLOUDINARY_API_SECRET=your-api-secret

# Stripe
STRIPE_API_KEY=sk_test_...
STRIPE_SECRET_KEY=sk_test_...

# Razorpay
RAZORPAY_API_KEY=rzp_test_...
RAZORPAY_SECRET=your-razorpay-secret

# Email (SMTP)
SMTP_HOST=smtp.mailtrap.io
SMTP_PORT=587
SMTP_USER=your-smtp-user
SMTP_PASS=your-smtp-password
```

4. **Start the server**

```bash
# Development mode (with hot reload)
npm run dev

# Production mode
npm start
```

5. **Access the API**

- API Base URL: `http://localhost:4000/api/v1`
- Swagger Docs: `http://localhost:4000/api-docs`

---

## ๐Ÿ“ Project Structure

```
tshirt-store-backend-api/
โ”œโ”€โ”€ config/
โ”‚ โ””โ”€โ”€ db.js # MongoDB connection
โ”œโ”€โ”€ controllers/
โ”‚ โ”œโ”€โ”€ homeController.js # Health check endpoints
โ”‚ โ”œโ”€โ”€ userController.js # User authentication & profile
โ”‚ โ”œโ”€โ”€ productController.js # Product CRUD & reviews
โ”‚ โ”œโ”€โ”€ orderController.js # Order management
โ”‚ โ””โ”€โ”€ paymentController.js # Payment processing
โ”œโ”€โ”€ middlewares/
โ”‚ โ”œโ”€โ”€ bigPromise.js # Async error wrapper
โ”‚ โ””โ”€โ”€ user.js # Auth & role middlewares
โ”œโ”€โ”€ models/
โ”‚ โ”œโ”€โ”€ user.js # User schema
โ”‚ โ”œโ”€โ”€ product.js # Product schema
โ”‚ โ””โ”€โ”€ order.js # Order schema
โ”œโ”€โ”€ routes/
โ”‚ โ”œโ”€โ”€ home.js # Home routes
โ”‚ โ”œโ”€โ”€ user.js # User routes
โ”‚ โ”œโ”€โ”€ product.js # Product routes
โ”‚ โ”œโ”€โ”€ order.js # Order routes
โ”‚ โ””โ”€โ”€ payment.js # Payment routes
โ”œโ”€โ”€ utils/
โ”‚ โ”œโ”€โ”€ cookieToken.js # JWT token helper
โ”‚ โ”œโ”€โ”€ customError.js # Custom error class
โ”‚ โ”œโ”€โ”€ emailHelper.js # Email sending utility
โ”‚ โ””โ”€โ”€ whereClause.js # Query builder for filtering
โ”œโ”€โ”€ views/
โ”‚ โ””โ”€โ”€ signuptest.ejs # Test view template
โ”œโ”€โ”€ app.js # Express app setup
โ”œโ”€โ”€ index.js # Server entry point
โ”œโ”€โ”€ swagger.yaml # API documentation
โ””โ”€โ”€ package.json
```

---

## ๐Ÿ”Œ API Endpoints

### Authentication

| Method | Endpoint | Description |
| ------ | ------------------------------- | ---------------------- |
| POST | `/api/v1/signup` | Register new user |
| POST | `/api/v1/login` | User login |
| GET | `/api/v1/logout` | User logout |
| POST | `/api/v1/forgotPassword` | Request password reset |
| POST | `/api/v1/password/reset/:token` | Reset password |

### User Profile

| Method | Endpoint | Description |
| ------ | ------------------------------ | ---------------- |
| GET | `/api/v1/userdashboard` | Get current user |
| POST | `/api/v1/userdashboard/update` | Update profile |
| POST | `/api/v1/password/update` | Change password |

### Products

| Method | Endpoint | Description |
| ------ | --------------------------- | ------------------------------- |
| GET | `/api/v1/products` | Get all products (with filters) |
| GET | `/api/v1/product/:id` | Get single product |
| GET | `/api/v1/reviews?id=` | Get product reviews |
| PUT | `/api/v1/review` | Add/Update review |
| DELETE | `/api/v1/review?productId=` | Delete review |

### Orders

| Method | Endpoint | Description |
| ------ | ---------------------- | ----------------- |
| POST | `/api/v1/order/create` | Create new order |
| GET | `/api/v1/order/:id` | Get order details |
| GET | `/api/v1/myorder` | Get user's orders |

### Payments

| Method | Endpoint | Description |
| ------ | ------------------------- | ----------------------- |
| GET | `/api/v1/stripekey` | Get Stripe public key |
| GET | `/api/v1/razorpaykey` | Get Razorpay public key |
| POST | `/api/v1/capturestripe` | Create Stripe payment |
| POST | `/api/v1/capturerazorpay` | Create Razorpay order |

### Admin Routes

| Method | Endpoint | Description |
| ------ | --------------------------- | ------------------- |
| GET | `/api/v1/admin/users` | Get all users |
| GET | `/api/v1/admin/user/:id` | Get user by ID |
| PUT | `/api/v1/admin/user/:id` | Update user |
| DELETE | `/api/v1/admin/user/:id` | Delete user |
| GET | `/api/v1/admin/products` | Get all products |
| POST | `/api/v1/admin/product/add` | Add product |
| PUT | `/api/v1/admin/product/:id` | Update product |
| DELETE | `/api/v1/admin/product/:id` | Delete product |
| GET | `/api/v1/admin/orders` | Get all orders |
| PUT | `/api/v1/admin/order/:id` | Update order status |
| DELETE | `/api/v1/admin/order/:id` | Delete order |

### Manager Routes

| Method | Endpoint | Description |
| ------ | ----------------------- | -------------------------- |
| GET | `/api/v1/manager/users` | Get users with 'user' role |

---

## ๐Ÿ”’ Authentication

The API supports two authentication methods:

### 1. Cookie Authentication

After login, a JWT token is automatically set as an httpOnly cookie named `token`.

### 2. Bearer Token

Pass the JWT token in the Authorization header:

```
Authorization: Bearer
```

---

## ๐Ÿงช Testing the API

### Using Swagger UI

Visit the [Swagger Documentation](https://tshirt-store.onrender.com/api-docs/) to test endpoints directly in your browser.

### Using cURL

```bash
# Register a new user
curl -X POST https://tshirt-store.onrender.com/api/v1/signup \
-F "name=John Doe" \
-F "email=john@example.com" \
-F "password=password123" \
-F "photo=@/path/to/photo.jpg"

# Login
curl -X POST https://tshirt-store.onrender.com/api/v1/login \
-H "Content-Type: application/json" \
-d '{"email":"john@example.com","password":"password123"}'

# Get all products
curl https://tshirt-store.onrender.com/api/v1/products
```

### Using Postman

Import the Swagger specification from `swagger.yaml` into Postman for a complete collection.

---

## ๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request

---

## ๐Ÿ“„ License

This project is licensed under the ISC License.

---

## ๐Ÿ‘จโ€๐Ÿ’ป Author

**Saket Kothari**

- Website: [saketkothari.vercel.app](https://saketkothari.vercel.app)\
- GitHub: [@SaketKothari](https://github.com/SaketKothari)

---

## โญ Show Your Support

Give a โญ๏ธ if this project helped you!