The Coupon Distribution System is a web application that distributes coupons to guest users in a round-robin manner while providing an admin panel to manage coupons and prevent abuse. The system consists of a React frontend and Express/MongoDB backend.
The application follows a client-server architecture:
- Frontend: React application with user and admin interfaces
- Backend: Node.js/Express API with MongoDB database
- Authentication: JWT-based authentication for admin access
- Coupon Claim Page: Allows guest users to claim coupons
- Cooldown Mechanism: Prevents users from claiming multiple coupons within 24 hours
- Feedback System: Provides clear messages for successful/failed claims
- Dashboard: Overview of system statistics
- Coupon Management: Create, update, and toggle coupon availability
- Claims History: View which users claimed which coupons
- IP Tracking: Prevents multiple claims from the same IP
- Cookie-Based Tracking: Restricts claims from the same browser session
- Cooldown System: Enforces a waiting period between claims
-
Create a new React application:
cd coupon-frontend -
Install required dependencies:
npm install react-router-dom axios react-toastify date-fns
-
Replace the source code with the provided files structure.
-
Configure API endpoint in
.env:REACT_APP_API_URL=http://localhost:3000/api -
Start the development server:
npm start
- CouponClaim: Core component that handles user coupon requests and displays results
- CooldownTimer: Shows remaining time until a user can claim another coupon
- AdminLogin: Authentication form for admin access
- AdminDashboard: Overview of system statistics
- AdminCoupons: Interface for managing coupons (add, edit, toggle status)
- AdminClaims: List of all coupon claims with details
- Admin enters credentials in the login form
- The system validates credentials against the backend
- Upon successful authentication, JWT token is stored in an HTTP-only cookie
- Protected routes use
ProtectedRoutecomponent to verify authentication
- Coupon Claiming: Sends a request to the backend API to claim a coupon
- Cooldown Management: Uses localStorage to track claim times along with server verification
- Admin Operations: Manage coupons through CRUD operations
-
Create a new Node.js project:
mkdir coupon-backend cd coupon-backend npm init -y -
Install required dependencies:
npm install express mongoose cookie-parser cors jsonwebtoken bcrypt uuid dotenv
-
Create the server and database models according to API documentation.
-
Configure environment variables in
.envfile:PORT=3000 MONGODB_URI=mongodb://localhost:27017/coupon-system JWT_SECRET=your_secure_jwt_secret ADMIN_USERNAME=admin ADMIN_PASSWORD=your_secure_password FRONTEND_URL=http://localhost:3001 -
Start the server:
node server.js
POST /api/coupons/claim: Claims a coupon for the user
POST /api/admin/login: Admin authenticationPOST /api/admin/logout: Admin logoutGET /api/admin/coupons: Get all couponsPOST /api/admin/coupons: Add a new couponPUT /api/admin/coupons/:id: Update coupon detailsGET /api/admin/claims: Get claim history
- JWT Authentication: Secure admin access with short-lived tokens
- HTTP-Only Cookies: Prevent client-side access to auth tokens
- CORS Configuration: Limit API access to trusted origins
- Input Validation: Sanitize all user inputs
- Rate Limiting: Prevent abuse with request limits
{
username: String, // required, unique
password: String, // hashed, required
isAdmin: Boolean // default: false
}{
code: String, // required, unique
description: String,
isActive: Boolean, // default: true
isUsed: Boolean, // default: false
createdAt: Date // default: current date
}{
couponId: ObjectId, // reference to Coupon
ipAddress: String, // required
sessionId: String, // required
claimedAt: Date // default: current date
}- User visits the homepage and clicks "Claim Coupon"
- Frontend sends a POST request to
/api/coupons/claim - Backend validates the request:
- Checks if the user has claimed a coupon in the last 24 hours
- Verifies IP and session information
- If valid, backend:
- Selects the oldest available coupon
- Marks it as used
- Records the claim details
- Frontend displays the coupon to the user or shows cooldown message
- Admin logs in through the admin login page
- Upon successful authentication, admin can access:
- Dashboard with system statistics
- Coupon management page
- Claims history page
- On the coupon management page, admin can:
- Add new coupons
- Edit existing coupons
- Toggle coupon availability
- Build the React application:
npm run build
- Deploy the build folder to a static hosting service:
- Netlify
- Vercel
- Firebase Hosting
- AWS S3 + CloudFront
-
Deploy the Node.js application to:
- Heroku
- AWS Elastic Beanstalk
- DigitalOcean App Platform
- Google Cloud Run
-
Set up a MongoDB database:
- MongoDB Atlas (cloud solution)
- Self-hosted MongoDB on a server
-
Configure environment variables in the hosting platform.
- Update CORS configuration to allow the production frontend domain
- Configure rate limiting for production environment
- Set up monitoring and logging
- Create a secure admin account with a strong password
- Database backups
- Log monitoring
- Security updates
- Performance optimization
- Email notifications for admins
- Coupon categories/tags
- Advanced analytics dashboard
- User accounts with personalized coupons
- Bulk import/export of coupons
- Expiration dates for coupons
- Coupon Claim Fails: Check IP tracking and cooldown settings
- Admin Authentication Issues: Verify JWT secret and cookie configuration
- Database Connection Problems: Check MongoDB connection string
- CORS Errors: Ensure frontend origin is allowed in backend configuration
- Browser developer tools for frontend issues
- Backend logs for API errors
- MongoDB Compass for database inspection
- Bundle size optimization
- Lazy loading of admin components
- Memoization of expensive computations
- Optimized rendering with React's useMemo and useCallback
- Database indexing
- Query optimization
- Connection pooling
- Caching frequently accessed data
The Coupon Distribution System provides a robust solution for distributing coupons in a controlled manner while preventing abuse. The combination of frontend user experience and backend security measures ensures a reliable system for both users and administrators.