A Next.js application demonstrating IP-based access control using Contentstack Edge Functions. This project shows how to implement IP whitelisting to restrict access to your application based on client IP addresses.
- IP Allowlist Protection: Restrict access to specific IP addresses only
- Edge Function Integration: Uses Contentstack Edge Functions for real-time IP validation
- Modern UI: Beautiful, responsive interface built with Next.js and Tailwind CSS
- Real-time Access Control: IP validation happens at the edge for optimal performance
- Node.js 18+
- npm or yarn
- Contentstack account (for Edge Functions deployment)
-
Clone the repository
git clone <repository-url> cd Edge-ip-whitelist-example-main
-
Install dependencies
npm install
-
Run the development server
npm run dev
-
Open your browser Navigate to http://localhost:3000
Edit the functions/[proxy].edge.js file to configure your allowed IP addresses:
const allowedIPs = [
"127.0.0.1", // Localhost
"::1", // IPv6 localhost
"192.168.1.100", // Your office IP
"10.0.0.50", // VPN IP
"172.16.0.10" // Internal network IP
];To add your current IP address to the whitelist:
- Visit whatismyipaddress.com to find your public IP
- Add it to the
allowedIPsarray infunctions/[proxy].edge.js - Deploy the Edge Function
Edge-ip-whitelist-example-main/
├── app/ # Next.js app directory
│ ├── page.tsx # Main application page
│ ├── layout.tsx # Root layout component
│ └── globals.css # Global styles
├── functions/
│ └── [proxy].edge.js # Edge Function for IP validation
├── public/ # Static assets
├── package.json # Dependencies and scripts
└── next.config.ts # Next.js configuration
- Request Interception: All requests are intercepted by the Edge Function
- IP Extraction: The client's IP address is extracted from request headers
- Validation: The IP is checked against the whitelist
- Access Control:
- ✅ Allowed: Request proceeds to the application
- ❌ Denied: Returns 403 Forbidden response
The Edge Function handles various IP scenarios:
- Direct client connections
- Requests behind proxies/load balancers
- IPv4 and IPv6 addresses
- Multiple forwarded IPs
const clientIP = request.headers.get("x-forwarded-for") || "";
const clientIPList = clientIP.split(",").map(ip => ip.trim());npm run devnpm run build
npm start- Deploy the Edge Function to Contentstack
- Configure the function to intercept your desired routes
- Test with different IP addresses
- Allowed IP: Should see the success page
- Restricted IP: Should see "Forbidden" message
- Local Development: Should work with
127.0.0.1
You can test the IP validation by:
- Using different network connections
- Using VPN services
- Modifying the
allowedIPsarray
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues or have questions:
- Check the Contentstack documentation
- Review the Edge Function logs in your Contentstack dashboard
- Ensure your IP address is correctly added to the whitelist
Note: This is an example implementation. In production, consider additional security measures such as rate limiting, logging, and monitoring.