auto-claude: subtask-2-3 - Add IP extraction utility for Next.js requests

This commit is contained in:
2026-01-25 11:55:45 +01:00
parent e39bb8951d
commit 40aafb04ab
+33
View File
@@ -0,0 +1,33 @@
import { NextRequest } from 'next/server';
/**
* Extract client IP address from Next.js request headers
*
* This function handles various proxy and forwarding scenarios:
* - Vercel's x-forwarded-for header (may contain multiple IPs)
* - x-real-ip header (direct client IP)
* - Fallback for development environments
*
* @param request - Next.js request object
* @returns Client IP address as a string, or 'unknown-ip' as fallback
*/
export function getClientIp(request: NextRequest): string {
// Check Vercel's forwarded IP header first
// In production, this is the most reliable source
const forwardedFor = request.headers.get('x-forwarded-for');
if (forwardedFor) {
// x-forwarded-for may contain multiple IPs in format: "client, proxy1, proxy2"
// The first IP is the original client IP
return forwardedFor.split(',')[0].trim();
}
// Check for real IP header (used by some proxies and load balancers)
const realIp = request.headers.get('x-real-ip');
if (realIp) {
return realIp;
}
// Fallback to a default identifier for development environments
// or when headers are not available
return 'unknown-ip';
}