A RESTful backend API for a full-stack hair oil ecommerce application built with Node.js, Express, and MongoDB.
The backend provides authentication, user management, product management, orders, Stripe payments, Stripe webhooks, email notifications, Cloudinary image uploads, admin functionality, analytics, and customer messages.
This repository contains the backend/API for the Hair Oil Ecommerce application.
It is designed to work with a separate React + Vite frontend.
- User authentication
- JWT authorization
- User registration
- User login
- Forgot Password
- Password reset
- Product CRUD
- Product image uploads
- Shopping orders
- Order management
- Stripe Checkout
- Stripe webhook processing
- Payment verification
- Order confirmation emails
- Admin dashboard APIs
- Analytics APIs
- User management
- Contact/message APIs
- User registration
- User login
- JWT authentication
- Protected routes
- Admin authorization
- Token expiration
- Forgot Password
- Password Reset
- Secure password hashing with bcrypt
- Email-based password reset
User clicks Forgot Password
β
Enters registered email
β
Backend finds user
β
Generate secure reset token
β
Hash token before database storage
β
Set token expiration
β
Send reset email
β
User opens reset link
β
Creates new password
β
Password is hashed
β
Reset token removed
β
User can login
The API supports:
- Create products
- Read products
- Update products
- Delete products
- Search products
- Product filtering
- Product pagination
- Product image uploads
- Cloudinary integration
The backend supports:
- Create orders
- Get user orders
- Get all orders
- Get individual order details
- Update order status
- Cancel pending orders
- Paid order tracking
- Payment information
- Shipping address information
The backend integrates with Stripe for secure online payments.
Payment flow:
Customer Checkout
β
Create Order
β
Create Stripe Checkout Session
β
Customer Pays
β
Stripe Processes Payment
β
Stripe Webhook
β
Backend Verifies Event
β
Order Marked Paid
β
Confirmation Email
Stripe webhook events are handled by the backend.
Important event:
checkout.session.completed
The webhook:
- Verifies the Stripe signature.
- Finds the matching order.
- Prevents duplicate processing.
- Marks the order as paid.
- Stores payment information.
- Saves the order.
- Sends the customer a confirmation email.
The backend uses Nodemailer with Gmail SMTP.
Email functionality includes:
- Password reset emails
- Order confirmation emails
- Customer notifications
The email sender is configured using environment variables.
Cloudinary is used for product image uploads.
The backend handles:
- Image upload
- Image storage
- Cloudinary URLs
- Product image references
Admin-protected APIs provide functionality for:
- Revenue statistics
- Total products
- Total users
- Total orders
- Inventory information
- Low-stock products
- Top-selling products
- Recent orders
- Recent users
- Create
- Update
- Delete
- Search
- Manage images
- View orders
- Search orders
- Update order status
- Cancel pending orders
- Export order information
- View users
- Search users
- Admin authorization
- Receive customer messages
- Manage customer messages
- Node.js
- Express.js
- JavaScript
- MongoDB
- Mongoose
- JSON Web Token
- bcryptjs
- crypto
- Stripe
- Stripe Webhooks
- Nodemailer
- Gmail SMTP
- Cloudinary
- Helmet
- CORS
- Environment variables
- JWT authentication
- Password hashing
- Stripe webhook signature verification
npm install express
npm install mongoose
npm install bcryptjs
npm install jsonwebtoken
npm install dotenv
npm install cors
npm install helmet
npm install nodemailer
npm install stripe
npm install cloudinary
npm install multer
npm install @getbrevo/brevoFor development:
npm install --save-dev nodemonbackend/
β
βββ config/
β βββ db.js
β
βββ controllers/
β βββ authController.js
β βββ productController.js
β βββ orderController.js
β βββ paymentController.js
β βββ adminController.js
β βββ contactController.js
β
βββ middleware/
β βββ authMiddleware.js
β
βββ models/
β βββ userModel.js
β βββ productModel.js
β βββ orderModel.js
β
βββ routes/
β βββ authRoutes.js
β βββ productsRoutes.js
β βββ orderRoutes.js
β βββ paymentRoutes.js
β βββ adminRoutes.js
β βββ contactRoutes.js
β βββ testEmailRoutes.js
β
βββ utils/
β βββ sendEmail.js
β βββ orderEmailTemplate.js
β
βββ public/
β βββ images/
β
βββ .env
βββ .gitignore
βββ package.json
βββ server.js
The API is organized into resource-based routes.
/api/auth
/api/products
/api/orders
/api/admin
/api/payment
/api/upload
/api/contact
/api/test-email
POST /api/auth/register
POST /api/auth/login
GET /api/auth/profile
POST /api/auth/forgot-password
POST /api/auth/reset-password/:token
Product routes provide functionality for:
GET
POST
PUT
DELETE
depending on the requested resource and authorization level.
Order APIs provide:
- Create order
- User order history
- Individual order details
- Admin order management
- Order status updates
Stripe-related endpoints handle:
- Checkout session creation
- Payment processing
- Stripe webhook events
- Payment confirmation
The Stripe webhook must receive the raw request body so that Stripe signature verification works correctly.
The backend allows the frontend application to communicate with the API through CORS configuration.
Development:
Frontend
http://localhost:5173
Backend
http://localhost:5000
Production URLs should be configured through environment variables.
The backend requires environment variables for sensitive configuration.
Example .env.example:
PORT=5000
MONGO_URI=your_mongodb_connection
JWT_SECRET=your_secret_key
CLOUDINARY_CLOUD_NAME=xxxx
CLOUDINARY_API_KEY=xxxx
CLOUDINARY_API_SECRET=xxxx
STRIPE_SECRET_KEY=sk_test_xxxxx
STRIPE_WEBHOOK_SECRET=whsec_xxxxx
EMAIL_USER=your_email@gmail.com
EMAIL_PASS=your_app_password
CLIENT_URL=http://localhost:5173git clone git@github.com:Faiza-tech/hairoilbackend.gitcd hairoilbackendnpm installCreate:
.env
and add your local environment variables.
If your package.json contains the development script:
npm run devThe backend runs on:
http://localhost:5000
npm startThe exact command depends on the scripts configured in package.json.
Before deployment, test:
- Register
- Login
- Wrong password
- Expired JWT
- Forgot Password
- Reset Password
- Product listing
- Search
- Filtering
- Admin create
- Admin edit
- Admin delete
- Create order
- User orders
- Admin orders
- Order status updates
- Checkout session
- Successful payment
- Stripe webhook
- Paid order update
- Confirmation email
- Password reset email
- Order confirmation email
The backend uses several security measures:
- Password hashing with bcrypt
- JWT authentication
- Protected routes
- Admin authorization
- Helmet security headers
- CORS
- Environment variables
- Stripe webhook signature verification
- Secure password reset tokens
- Expiring password reset links
CUSTOMER
β
βΌ
React + Vite Frontend
β
β Axios / REST API
βΌ
Node.js + Express API
β
ββββββββββββββΌβββββββββββββ
β β β
βΌ βΌ βΌ
MongoDB Stripe Cloudinary
β β β
β βΌ β
β Stripe Webhook β
β β β
ββββββββββββββΌβββββββββββββ
β
βΌ
Nodemailer
β
βΌ
Customer Email
Through this backend project I worked with:
- Node.js
- Express
- MongoDB
- Mongoose
- REST API design
- JWT authentication
- bcrypt password hashing
- Password reset systems
- Email workflows
- Nodemailer
- Stripe Checkout
- Stripe webhooks
- Payment verification
- Cloudinary
- CRUD APIs
- Admin authorization
- Middleware
- CORS
- Helmet
- Environment variable management
- Error handling
- Database relationships
- Ecommerce order processing
Potential improvements include:
- Refresh token authentication
- Rate limiting
- Advanced request validation
- More detailed audit logging
- Product reviews API
- Wishlist API
- Coupon and discount API
- Inventory reservation
- Advanced analytics
- Automated testing
- API documentation with Swagger
- Production monitoring
- More advanced email templates
Frontend repository:
https://github.com/Faiza-tech/hairoilFrontend
Faiza
If you find this project helpful, feel free to β the repository.