Skip to content

About

Full Stack eCommerce store

Topics

Resources

Stars

19 stars

Watchers

3 watching

Forks

Repository files navigation

MERN Commerce

A full-stack e-commerce solution built with the MERN stack (MongoDB, Express.js, React.js, Next.js) and TypeScript. This application provides a complete e-commerce platform with user authentication, product management, order processing, and an admin panel.

πŸš€ Technology Stack

Backend

  • Node.js - JavaScript runtime environment
  • Express.js - Web application framework
  • TypeScript - Type-safe JavaScript
  • MongoDB - NoSQL database
  • Mongoose - MongoDB object modeling
  • JWT - JSON Web Tokens for authentication
  • bcrypt - Password hashing
  • AdminBro - Admin panel interface

Frontend

  • React.js - UI library
  • Next.js - React framework for production
  • TypeScript - Type-safe JavaScript

DevOps

  • Docker - Containerization
  • Heroku - Cloud platform (Procfile included)

✨ Features

User Management

  • User registration and authentication
  • JWT-based authentication
  • Role-based access control (superadmin, admin, user)
  • Password encryption with bcrypt
  • User profile management

Product Management

  • Create, read, update, and delete products
  • Product search functionality
  • Category-based product filtering
  • Product slug generation
  • Bulk product generation from external API (FakeStore API)
  • Category management

Order Management

  • Create and manage orders
  • Order status tracking (pending, accepted, delivered, rejected)
  • Order reports and analytics
  • Order history with populated product details
  • Shipping address management

Admin Features

  • Admin panel powered by AdminBro
  • Role-based authorization
  • Order status management
  • User management
  • Product management
  • Order reports and analytics

πŸ“ Project Structure

mern-commerce/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ app.ts                 # Express app configuration
β”‚   β”œβ”€β”€ server.ts              # Server entry point
β”‚   β”œβ”€β”€ components/            # React components
β”‚   β”œβ”€β”€ controllers/           # Business logic controllers
β”‚   β”‚   β”œβ”€β”€ accountController.ts
β”‚   β”‚   β”œβ”€β”€ orderController.ts
β”‚   β”‚   └── productController.ts
β”‚   β”œβ”€β”€ interfaces/            # TypeScript interfaces
β”‚   β”œβ”€β”€ middlewares/           # Express middlewares
β”‚   β”‚   β”œβ”€β”€ authMiddleware.ts
β”‚   β”‚   └── logger.ts
β”‚   β”œβ”€β”€ models/                # Mongoose models
β”‚   β”‚   β”œβ”€β”€ orderModel.ts
β”‚   β”‚   β”œβ”€β”€ productModel.ts
β”‚   β”‚   └── userModel.ts
β”‚   β”œβ”€β”€ pages/                 # Next.js pages
β”‚   └── routes/                # API routes
β”‚       β”œβ”€β”€ accountRoutes.ts
β”‚       β”œβ”€β”€ orderRoutes.ts
β”‚       └── productRoutes.ts
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ Procfile
β”œβ”€β”€ package.json
└── tsconfig.json

πŸ› οΈ Installation

Prerequisites

  • Node.js (v14.16.0 or higher)
  • MongoDB (local or cloud instance)
  • npm or yarn

Setup

  1. Clone the repository

    git clone <repository-url>
    cd mern-commerce
  2. Install dependencies

    npm install
  3. Environment Variables Create a .env file in the root directory:

    PORT=5000
    DB_STRING=mongodb://127.0.0.1:27017/mern-commerce
    SECRET_KEY=your-secret-key-here
    NODE_ENV=development
  4. Start the development server

    npm run dev

    The server will start on http://localhost:5000

πŸ“œ Available Scripts

  • npm run dev - Start development server with nodemon
  • npm run build:server - Build TypeScript server code
  • npm run build:client - Build Next.js client
  • npm run build - Build both client and server
  • npm start - Start production server (requires build)

πŸ”Œ API Endpoints

Authentication & User Management

Register User

POST /api/register
Body: {
  firstName: string,
  lastName: string,
  username: string,
  email: string,
  password: string
}

Login

POST /api/login
Body: {
  username: string,
  password: string
}
Response: { token: string }

Get Current User

GET /api/user
Headers: Authorization: Bearer <token>

Get All Users (Admin only)

GET /api/users
Headers: Authorization: Bearer <token>

Update User (Admin only)

PUT /api/users
Headers: Authorization: Bearer <token>
Body: { _id: string, ...userData }

Delete User (Admin only)

DELETE /api/users
Headers: Authorization: Bearer <token>
Body: { _id: string }

Product Management

Get All Products

GET /api/products

Get Single Product

GET /api/product?slug=product-slug

Search Products

GET /api/product/search?q=search-query

Get Products by Category

GET /api/category/:categorySlug

Get All Categories

GET /api/products/categories

Create Product (Admin only)

POST /api/products
Headers: Authorization: Bearer <token>
Body: {
  title: string,
  description: string,
  price: number,
  category: string,
  image: string
}

Update Product (Admin only)

PUT /api/products
Headers: Authorization: Bearer <token>
Body: { _id: string, ...productData }

Delete Product (Admin only)

DELETE /api/products
Headers: Authorization: Bearer <token>
Body: { _id: string }

Generate Products from API (Superadmin only)

GET /api/products/generate
Headers: Authorization: Bearer <token>

Add Category

POST /api/products/categories
Body: { title: string }

Order Management

Get All Orders (Admin only)

GET /api/orders
Headers: Authorization: Bearer <token>

Get Order by ID

GET /api/order?id=ORDER_ID
Headers: Authorization: Bearer <token>

Create Order

POST /api/orders
Headers: Authorization: Bearer <token>
Body: {
  shippingAddress: string,
  orderEmail: string,
  orderItems: [
    { productId: string, quantity: number }
  ]
}

Update Order Status (Admin only)

PUT /api/orders
Headers: Authorization: Bearer <token>
Body: {
  _id: string,
  status: "pending" | "accepted" | "delivered" | "rejected"
}

Delete Order (Admin only)

DELETE /api/orders
Headers: Authorization: Bearer <token>
Body: { _id: string }

Generate Order Report (Superadmin only)

GET /api/orders/report
Headers: Authorization: Bearer <token>

πŸ” Authentication

The API uses JWT (JSON Web Tokens) for authentication. Include the token in the Authorization header:

Authorization: Bearer <your-token>

User Roles

  • user - Regular customer (default)
  • admin - Can manage products and orders
  • superadmin - Full access including user management and reports

🐳 Docker Deployment

  1. Build the Docker image

    docker build -t mern-commerce .
  2. Run the container

    docker run -p 5000:5000 --env-file .env mern-commerce

πŸš€ Production Deployment

Heroku

The project includes a Procfile for Heroku deployment:

  1. Create a Heroku app
  2. Set environment variables in Heroku dashboard
  3. Deploy using Git:
    git push heroku main

Environment Variables for Production

  • PORT - Server port (Heroku sets this automatically)
  • DB_STRING - MongoDB connection string
  • SECRET_KEY - JWT secret key
  • NODE_ENV - Set to "production"

πŸ“ Database Models

User Model

  • firstName, lastName, username, email, password
  • role (default: "user")
  • timestamp

Product Model

  • title, description, price, slug, category, image
  • createdAt

Order Model

  • customer (User reference)
  • shippingAddress, orderEmail
  • status (default: "pending")
  • orderNo (auto-increment)
  • orderItems (virtual reference to OrderItem)
  • timestamp

OrderItem Model

  • product (Product reference)
  • order (Order reference)
  • price, quantity
  • timestamp

πŸ›‘οΈ Security Features

  • Password hashing with bcrypt
  • JWT token-based authentication
  • Role-based authorization
  • Input validation
  • CORS enabled
  • Environment variable configuration

πŸ“„ License

ISC

πŸ‘€ Author

Shakil Ahmed

🀝 Contributing

Contributions, issues, and feature requests are welcome!

πŸ“ž Support

For support, please open an issue in the repository.


Made with ❀️ by Shakil Ahmed

About

Full Stack eCommerce store

Topics

Resources

Stars

19 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages