A full-stack e-commerce application built with MERN stack (MongoDB, Express.js, React, Node.js) that demonstrates modern web development practices including authentication, authorization, data validation, and error handling.
Clone the repository
git clone [repository-url]
cd [project-name]
Backend setup
cd backend
npm install
DB_CONNECT = mongodb://
node server.js
3. Frontend setup
```bash
cd frontend
npm install
npm run dev
POST /api/users/register # Create new user account
POST /api/users/login # Get authentication token
POST /api/users/logout # Clear authentication token
GET /api/users # List all users (admin)
GET /api/users/:id # Get user details
PUT /api/users/:id # Update user
DELETE /api/users/:id # Delete user
POST /api/products # Create product (admin)
GET /api/products # List all products
GET /api/products/actual # List in-stock products
GET /api/products/:id # Get product details
PUT /api/products/:id # Update product (admin)
DELETE /api/products/:id # Delete product (admin)
POST /api/users/:id/cart # Add to cart
GET /api/users/:id/cart # View cart
POST /api/users/:id/cart/clear # Clear cart
POST /api/users/:id/orders # Create order from cart
GET /api/orders # List all orders (admin)
GET /api/orders/:id # Get order details
PATCH /api/orders/:id # Update order status
{
firstName: String, // Required, letters and hyphens, 1-50 chars
lastName: String, // Required, letters and hyphens, 1-50 chars
username: String, // Required, unique, lowercase letters and numbers
password: String, // Required, hashed, min 8 chars with complexity rules
role: String, // "user" or "admin"
email: String, // Required, unique, valid email format
phone: String, // Required, unique, international format (+numbers)
country: String, // Required, letters and spaces
city: String, // Required, letters and spaces
street: String, // Required, letters and spaces
house: Number, // Required, positive integer
apartment: Number, // Optional, positive integer
cart: [{ productId, quantity }]
}
{
name: String, // Required, unique, 3-100 chars
description: String, // Required, 10-1000 chars
price: Number, // Required, positive
quantity: Number, // Required, integer, min 0
categoryId: ObjectId,// Required, reference to Category
imagePath: String, // Required, PNG/JPG/JPEG path
isEnded: Boolean // Out of stock flag
}
{
name: String, // Required, unique, 2-50 chars, alphanumeric
description: String // Optional, 10-500 chars
}
{
userId: ObjectId, // Reference to User
products: [{ // Array of ordered products
productId: ObjectId,
quantity: Number,
priceAtPurchase: Number
}],
status: String, // pending/shipping/completed/cancelled
totalPrice: Number,
createdAt: Date
}
res.cookie("token", token, {
httpOnly: true, // Prevents JavaScript access
maxAge: 3600000, // 1 hour expiration
// secure: true, // Uncomment in production (HTTPS only)
// sameSite: 'strict' // CSRF protection
});
The API endpoints have been tested using Postman.
Enable cookie handling in Postman:
Testing authentication flow:
Authentication Flow:
Authorization Tests:
This is an educational project open for contributions. Areas where you can contribute:
Code Refactoring
Frontend Development
Testing
Documentation
git checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)Backend .env
file in src/config/.env
:
DB_CONNECT = mongodb://<username>:<password>@host:port/dbname
PORT = 3000
SALT_ROUNDS = 1
JWT_SECRET = "your_secret_key"
The project includes initial data setup with categories, products, and user accounts.
Default Categories:
Default Users:
// Admin account
{
username: "admin",
password: "Pa$$word123",
role: "admin"
}
// Regular user account { username: "user1", password: "Pa$$word123", role: "user" }
3. Sample products in each category (books, albums, movies, tools, plants)
To initialize the database with sample data:
```bash
# From the backend directory
npm run init-db
This will:
cd backend
npm run init-db
npm install
node server.js
cd frontend
npm install
npm run dev
This project demonstrates several key concepts in modern web development:
Backend Development
Security Practices
Database Design
API Development
For support, please:
MIT License
Copyright (c) 2024
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.