Skip to content

Latest commit

Β 

History

19 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›οΈ Enterprise Full-Stack E-Commerce Store & Admin Dashboard

React Node.js Express MongoDB JWT Dual-Token Axios Interceptor Stripe TailwindCSS Cloudinary License

πŸ” Featured Security Architecture: Enterprise Dual-Token Authentication (Short-lived Access Tokens + 7-day Refresh Tokens) stored in secure httpOnly, sameSite: "strict" cookies with Axios Queue-Locked Interceptors for transparent, silent background token renewals.

A production-grade, enterprise full-stack E-Commerce Storefront & Admin Dashboard built from scratch using React 19, Vite, Node.js, Express 5, MongoDB Atlas, Stripe (INR), Cloudinary, and Tailwind CSS with JWT Dual Token-Rotation Auth!.


πŸ—οΈ System Architecture

graph TD
    User[React 19 Client - Port 5173] <-->|HttpOnly Cookies + Axios Interceptor| Express[Express Node.js Server - Port 5000]
    
    subgraph Security Layer
        Express -->|protectRoute| Auth[JWT Dual Token Validation]
        Express -->|adminRoute| RBAC[Role-Based Access Control]
    end
    
    subgraph Data & Cloud Services
        Express <-->|Mongoose ODM| Mongo[(MongoDB Atlas)]
        Express <-->|Image Upload & Destroy| Cloudinary[Cloudinary CDN]
        Express <-->|INR Checkout Sessions| Stripe[Stripe API Gateway]
    end
Loading

🌟 Key Features

πŸ‘‘ Admin Dashboard (/secret-dashboard)

  • Real-Time KPI Stat Cards: Overview of Total Users, Catalog Products, Total Orders, and Gross Revenue.
  • Visual Sales Analytics: Interactive 7-day revenue and order trend line charts powered by Recharts.
  • Cloudinary Product Creator: Form accepting image uploads converted directly to Cloudinary CDN URLs.
  • Inventory Data Table: Browse, search, toggle featured homepage status, and delete products (which automatically destroys cloud assets on Cloudinary).

πŸ”’ Enterprise Security & Data Protection

  • Dual-Token Authentication: Short-lived Access Tokens (15m) + Long-lived Refresh Tokens (7d) stored in httpOnly, sameSite: "strict", and secure cookies.
  • Silent Token Auto-Refresh: Axios response interceptor with queue locking (isRefreshing + failedQueue) preventing duplicate token refresh requests.
  • Password Cryptography: Passwords salted and hashed using bcryptjs (10 rounds) via Mongoose pre("save") hooks.
  • Anti-Price Tampering: Server-side product price lookups directly from MongoDB during Stripe session generation.
  • Order Idempotency: Order.findOne({ stripeSessionId }) prevents duplicate order creation on page refreshes.

πŸ›’ Customer Storefront

  • Dynamic Home Hero Slider: Auto-sliding carousel showcasing featured products.
  • Category Filtering: Browse items by category (shoes, clothing, electronics, accessories).
  • Random Recommendation Engine: MongoDB $sample aggregation displaying random product recommendations ("You May Also Like").
  • Persistent Database Cart: Cart items saved directly to MongoDB (user.cartItems), persisting across user devices and sessions.
  • Stripe INR Checkout: PCI-compliant payment checkout processing amounts in Indian Rupees (β‚Ή) with paise precision.
  • Dynamic Delivery Fee Engine: Free delivery on orders $\ge$ β‚Ή500, otherwise applies a β‚Ή50 shipping fee.
  • Coupon System: Single-use reward gift coupons and global promo codes (WELCOME10, SAVE15, NEXUS10) with automatic expiration checks.
  • Order History: View complete past orders with itemized receipts and payment references.

πŸ› οΈ Tech Stack

Domain Technologies Used
Frontend Core React 19, Vite, React Router DOM v7
Styling & Motion Tailwind CSS, Framer Motion, Lucide Icons, Canvas Confetti
State Management Zustand (User, Cart, Product, Theme stores)
HTTP & Toast Axios, React Hot Toast
Charts & Visualization Recharts
Backend Runtime Node.js, Express 5
Database & ODM MongoDB Atlas, Mongoose 9
Cloud Storage Cloudinary SDK v2
Payments Stripe Node.js SDK (INR / Paise)
Security JSON Web Tokens (jsonwebtoken), bcryptjs, cookie-parser, cors

πŸ“ Directory Structure

E-COM/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   └── db.js                 # MongoDB Mongoose connection manager
β”‚   β”œβ”€β”€ controllers/
β”‚   β”‚   β”œβ”€β”€ analyticsController.js # Sales aggregation & 7-day chart pipelines
β”‚   β”‚   β”œβ”€β”€ authController.js      # Signup, login, logout, refresh token, profile
β”‚   β”‚   β”œβ”€β”€ cartController.js      # Cart CRUD & batch $in product lookup
β”‚   β”‚   β”œβ”€β”€ couponController.js    # Coupon validation & expiration auto-deactivation
β”‚   β”‚   β”œβ”€β”€ paymentController.js   # Stripe Checkout sessions in INR & Order creation
β”‚   β”‚   └── productController.js   # Product CRUD, Cloudinary upload & $sample recommendations
β”‚   β”œβ”€β”€ lib/
β”‚   β”‚   β”œβ”€β”€ cloudinary.js         # Cloudinary SDK configuration
β”‚   β”‚   β”œβ”€β”€ stripe.js             # Stripe SDK configuration
β”‚   β”‚   └── tokens.js             # JWT token generator & HttpOnly cookie setter
β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   └── authMiddleware.js     # protectRoute & adminRoute middleware
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ couponModel.js        # Coupon Schema
β”‚   β”‚   β”œβ”€β”€ orderModel.js         # Order Schema
β”‚   β”‚   β”œβ”€β”€ productModel.js       # Product Schema
β”‚   β”‚   └── userModel.js          # User Schema with bcrypt hooks
β”‚   β”œβ”€β”€ routes/                   # Express API route bindings
β”‚   β”œβ”€β”€ .env                      # Environment variables configuration
β”‚   β”œβ”€β”€ package.json
β”‚   └── server.js                 # Express server entry point
└── frontend/
    β”œβ”€β”€ src/
    β”‚   β”œβ”€β”€ components/           # Reusable UI components (Navbar, Footer, HeroCarousel, etc.)
    β”‚   β”œβ”€β”€ lib/
    β”‚   β”‚   └── axios.js          # Custom Axios instance with queue-locked refresh interceptor
    β”‚   β”œβ”€β”€ pages/                # Page views (HomePage, AdminPage, CartPage, etc.)
    β”‚   β”œβ”€β”€ stores/               # Zustand state stores (useUserStore, useCartStore, etc.)
    β”‚   β”œβ”€β”€ App.jsx               # Application routes & layout
    β”‚   β”œβ”€β”€ main.jsx              # React DOM entry point
    β”‚   └── index.css             # Tailwind CSS & custom utilities
    β”œβ”€β”€ package.json
    └── vite.config.js

πŸš€ Getting Started

Prerequisites

  • Node.js v18+ installed
  • MongoDB Atlas account & connection URI
  • Stripe account (Test Mode secret key)
  • Cloudinary account (Cloud Name, API Key, API Secret)

1. Setup Backend

cd backend
npm install

Create a .env file inside backend/:

PORT=5000
MONGO_URI=your_mongodb_atlas_connection_string
NODE_ENV=development

# Authentication JWT Secrets
ACCESS_TOKEN_SECRET=your_super_secret_access_token_key
REFRESH_TOKEN_SECRET=your_super_secret_refresh_token_key

# Cloudinary Configuration
CLOUDINARY_CLOUD_NAME=your_cloudinary_cloud_name
CLOUDINARY_API_KEY=your_cloudinary_api_key
CLOUDINARY_API_SECRET=your_cloudinary_api_secret

# Stripe Payment Gateway (Test Mode)
STRIPE_SECRET_KEY=sk_test_51...

# Client Origin
CLIENT_URL=http://localhost:5173

Start the backend development server:

npm run dev

2. Setup Frontend

Open a new terminal window:

cd frontend
npm install

Start the Vite development server:

npm run dev

Open your browser at http://localhost:5173 πŸŽ‰!


πŸ”— API Endpoint Reference

Authentication (/api/auth)

  • POST /api/auth/signup - Register a new customer account
  • POST /api/auth/login - Authenticate user & receive HttpOnly cookies
  • POST /api/auth/logout - Revoke cookies
  • POST /api/auth/refresh-token - Issue fresh access token cookie
  • GET /api/auth/profile - Fetch current user profile (Protected)

Products (/api/products)

  • GET /api/products/featured - Get products flagged isFeatured: true
  • GET /api/products/category/:category - Get products by category slug
  • GET /api/products/recommendations - Get 4 random products via $sample
  • GET /api/products - Get all products (Admin Only)
  • POST /api/products - Create product & upload image to Cloudinary (Admin Only)
  • PATCH /api/products/:id - Toggle product featured status (Admin Only)
  • DELETE /api/products/:id - Delete product & destroy Cloudinary image (Admin Only)

Cart (/api/cart)

  • GET /api/cart - Get user's cart products with merged details (Protected)
  • POST /api/cart - Add item to cart or increment quantity (Protected)
  • PUT /api/cart/:id - Update quantity or remove if 0 (Protected)
  • DELETE /api/cart - Remove single item or clear entire cart (Protected)

Coupons (/api/coupons)

  • GET /api/coupons - Fetch active coupon assigned to logged-in user (Protected)
  • POST /api/coupons/validate - Validate coupon code & return discount % (Protected)

Payments (/api/payments)

  • POST /api/payments/create-checkout-session - Create Stripe Checkout session in INR (Protected)
  • POST /api/payments/checkout-success - Verify payment & save Order document (Protected)

Analytics (/api/analytics)

  • GET /api/analytics - Fetch business KPIs & 7-day sales chart metrics (Admin Only)

πŸ“œ License

Distributed under the MIT License. See LICENSE for more information.

About

A production-grade, enterprise full-stack E-Commerce Storefront & Admin Dashboard built from scratch using MERN, Stripe API with JWT Dual Token-Rotation Auth for Security!!.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages