This is a modern, full-stack e-commerce application built with a hybrid architecture, combining Next.js for the frontend and a multi-database backend (Supabase and MongoDB). The application features a custom authentication system, comprehensive product management, shopping cart functionality, checkout process, and order management.
email: admin@example.com password: admin123
You can create your own credentials by filling registration form.
- Framework: Next.js with React
- Styling: Tailwind CSS for responsive design
- State Management: React Context API and custom hooks
- Authentication: Custom implementation using JWT tokens and localStorage
- Parallel Routing: Parallel routes for app skeleton such that whole app dont have to rerender when a specific slot is changed
- Server: Node.js with Express.js
- Authentication: JWT-based with custom middleware
- Primary Database: Supabase (PostgreSQL)
- Legacy Database: MongoDB (for specific collections)
- API Design: RESTful endpoints with proper error handling
- Custom implementation replacing Next-Auth
- JWT-based with token storage in localStorage
- Protected routes with AuthOnly wrapper component
- User registration, login, and profile management
- Browsing products by category
- Product details view with descriptions and pricing
- Advanced filtering and search capabilities
- Admin panel for product management (CRUD operations)
- Client-side cart state management
- Add/remove items and change quantities
- Persistent cart data between sessions
- Real-time price calculations
- Secure order placement with proper validation
- Integration with database for order storage
- Support for both SQL and NoSQL database IDs
- Error handling and transaction management
- Order history for users
- Detailed view of individual orders
- Status tracking for orders
- Admin reporting on sales and revenue
The application uses a hybrid database approach:
- Users
- Orders
- Order Items
- Analytics data
- Products and product metadata
- Legacy data still in transition
The application includes custom functionality to handle the differences between MongoDB's ObjectID format and Supabase's UUID format, specifically:
- SQL functions to handle MongoDB-style IDs in PostgreSQL
- Type conversions between different ID formats
- Fallback mechanisms for database operations
- User credentials are validated against the backend
- JWT token is generated and stored in localStorage
- Token is included in API requests via Authorization header
- Protected routes check for valid tokens before allowing access
- Cart items are validated
- Order is created in the database
- Order items are linked to the order with proper ID handling
- User is redirected to order confirmation
- Cart is cleared after successful checkout
- Node.js (v14 or higher)
- NPM or Yarn
- Supabase account and project
- MongoDB instance (optional)
- Environment variables properly configured
Create a .env
file in both the client
and backend
directories with the following variables:
# Backend .env
PORT=5040
JWT_SECRET=your_jwt_secret
SUPABASE_URL=your_supabase_url
SUPABASE_ANON_KEY=your_supabase_anon_key
MONGODB_URI=your_mongodb_uri
# Client .env
NEXT_PUBLIC_API_URL=http://localhost:5040/api
-
Clone the repository
git clone [repository-url] cd ecommerce-app
-
Install dependencies
# Backend cd backend npm install # Client cd ../client npm install
-
Database Setup
cd ../backend node setup.js
This script will set up the necessary database functions and schema modifications in Supabase.
-
Start the application
# Backend (in one terminal) cd backend npm run dev # Client (in another terminal) cd client npm run dev
-
Access the application
- Frontend: http://localhost:3000
- Backend API: http://localhost:5040/api
- Register a new user account
- Browse products and add items to cart
- Proceed to checkout and place an order
- View order history and details
If you encounter issues with checkout, especially related to UUID validation:
- Run the setup script:
node backend/setup.js
- Access the setup endpoint:
http://localhost:5040/api/admin/setup-database
- Check the database schema via:
http://localhost:5040/api/orders/check-schema
If authentication is not working:
- Check localStorage for the auth token in browser dev tools
- Verify JWT_SECRET is consistent between environments
- Check expiration time on the token
- id (UUID, primary key)
- username (string)
- email (string, unique)
- password (string, hashed)
- created_at (timestamp)
- updated_at (timestamp)
- id (UUID, primary key)
- user_id (UUID, foreign key)
- total_amount (decimal)
- status (enum: pending, completed, cancelled)
- created_at (timestamp)
- updated_at (timestamp)
- id (UUID, primary key)
- order_id (UUID, foreign key)
- product_id (text, stores MongoDB-style IDs)
- product_name (string)
- price (decimal)
- quantity (integer)
- created_at (timestamp)
- updated_at (timestamp)
- Challenge: MongoDB uses string IDs while Supabase expects UUIDs
- Solution: Custom SQL functions to handle string IDs in Supabase, with automatic schema modification
- Challenge: Moving from Next-Auth to custom JWT implementation
- Solution: Custom AuthProvider and useAuth hook for consistent auth state management
- Challenge: Ensuring data consistency during checkout
- Solution: Fallback mechanisms and cleanup procedures if any part of the transaction fails
- Payment gateway integration
- Real-time order tracking
- Enhanced analytics dashboard
- Product recommendation engine
- Complete migration from MongoDB to Supabase
Note: Due to lack of time, I did not host the backend service to any hosting provider, neither did I host the front end to any provider.
Although, Here I am writing down all the steps necessary to deploy the services to cloud.
I mostly use render or amplify for deploying backend services. Render provides a free tier which is straight forward to use. Amplify is a paid service that requires more configuration but is a long term configuration.
I use vercel for all of my frontend deployments because it provides good dx and creating CI/CD on vercel is straight forward. We need to give access to github repo to vercel, And then select the branch we want to configure, then add .env variables and any build commands if present whichresults in a deployed url we get from the platform.
For any questions or issues during testing, please contact me at [vishesh.bajpayee@icloud.com].
Thank you for reviewing this e-commerce application. I look forward to your feedback!