Skip to content

safwan-ms/MERN-Stack-SafwaxStore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

135 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

E-Commerce Store

Overview

This is a full-stack eCommerce store built using modern web technologies. It allows users to browse products, filter them by categories and brands, and make purchases. The store features authentication, a shopping cart, and an admin panel for managing products and orders.

Features

  • User authentication (Sign up, Login, Logout)
  • Product listing with category and brand filters
  • Shopping cart functionality
  • Checkout process with payment integration
  • Admin panel for product and order management
  • Responsive design for mobile and desktop

Tech Stack

Frontend:

React Vite Redux TailwindCSS React Router

Backend:

NodeJS Express.js MongoDB JWT

Installation & Setup

Prerequisites:

  • Node.js & npm installed
  • MongoDB running locally or on a cloud provider

Steps:

  1. Clone the repository:

    git clone https://github.com/safwan-ms/MERN-Stack-SafwaxStore.git
    cd MERN-Stack-SafwaxStore
  2. Install dependencies for frontend and backend:

    cd frontend  # Navigate to frontend folder
    npm install or npm i # Install frontend dependencies
    cd ../backend  # Navigate to backend folder
    npm install or npm i # Install backend dependencies
  3. Set up environment variables:

    • Create a .env file in the backend directory and add the required configurations:

      MONGO_URI=your_mongodb_connection_string
      JWT_SECRET=your_secret_key
  4. Start the development servers:

    npm run backend # Start backend server
    npm run frontend # Start frontend server
  5. Access the application:

    • Open http://localhost:5173 in your browser for the frontend.
    • Backend runs on http://localhost:5000 (or specified port).

API Routes Documentation (Backend)

User Routes

Base URL: /api/users

Method Endpoint Description
POST / Create a new user
GET / Get all users (Admin only)
POST /auth Login a user
POST /logout Logout the current user
GET /profile Get the current user's profile
PUT /profile Update the current user's profile
DELETE /:_id Delete a user by ID (Admin only)
GET /:_id Get a user by ID (Admin only)
PUT /:_id Update a user by ID (Admin only)

Middleware Used:

  • authenticate: Ensures only authenticated users can access certain routes.
  • authorizedAdmin: Restricts access to admin users only.

Product Routes

Base URL: /api/products

Method Endpoint Description
GET / Fetch paginated products
POST / Add a new product (Admin only, uses formidable for file uploads)
GET /allproducts Fetch all products without pagination
POST /:id/reviews Add a review to a product (Authenticated users only)
GET /top Fetch top-rated products
GET /new Fetch newly added products
GET /:id Fetch a product by ID
PUT /:id Update product details (Admin only, uses formidable for file uploads)
DELETE /:id Delete a product by ID (Admin only)
POST /filtered-products Filter products based on criteria

Middleware Used:

  • authenticate: Ensures only authenticated users can access certain routes.
  • authorizedAdmin: Restricts access to admin users only.
  • checkId: Validates product ID before performing actions.
  • formidable(): Handles file uploads for product images.

Category Routes

Base URL: /api/categories

Method Endpoint Description
POST / Create a new category (Admin only)
PUT /:categoryId Update a category (Admin only)
DELETE /:categoryId Remove a category (Admin only)
GET /categories List all categories
GET /:id Read a category by ID

Middleware Used:

  • authenticate: Ensures only authenticated users can access certain routes.
  • authorizedAdmin: Restricts access to admin users only.

Order Routes

Base URL: /api/orders

Method Endpoint Description
POST / Create a new order (Authenticated users only)
GET / Get all orders (Admin only)
GET /mine Get the current user's orders
GET /total-orders Get total number of orders
GET /total-sales Calculate total sales
GET /total-sales-by-date Calculate total sales by date
GET /:id Get an order by ID (Authenticated users only)
PUT /:id/pay Mark an order as paid (Authenticated users only)
PUT /:id/deliver Mark an order as delivered (Admin only)

Middleware Used:

  • authenticate: Ensures only authenticated users can access certain routes.
  • authorizedAdmin: Restricts access to admin users only (where applicable).

Notes

  • All admin routes require authentication and admin authorization.
  • Users can only modify their own profiles unless they have admin access.
  • Product and category-related operations (add, update, delete) are restricted to admins.

Example API Requests

Login a User

POST /api/users/auth
Content-Type: application/json
{
  "email": "user@example.com",
  "password": "password123"
}

Fetch All Products

GET /api/products/allproducts

Add a New Product (Admin Only)

POST /api/products
Authorization: Bearer <admin-token>
Content-Type: multipart/form-data
{
  "name": "New Product",
  "image": "<file>",
  "brand": "Brand Name",
  "price": 100,
  "quantity": 10,
  "category": "category_id",
  "description": "Product description",
  "countInStock": 50
}

Create a New Category (Admin Only)

POST /api/categories
Authorization: Bearer <admin-token>
Content-Type: application/json
{
  "name": "Electronics"
}

Contributing

Contributions are welcome! Feel free to fork the repository and submit pull requests.


Developed by Safwax

About

An eCommerce website is an online platform for buying πŸ›οΈ and selling products, with features like product listings, carts, and secure payments πŸ’³.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages