Skip to content

Commit 8ff8776

Browse files
feat: Make CORS more flexible to support multiple domains (Netlify, Vercel, localhost)
1 parent 2bd0dc5 commit 8ff8776

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

next.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const nextConfig = {
1414
{
1515
source: '/api/:path*',
1616
headers: [
17+
// Allow multiple specific domains for flexibility
1718
{ key: 'Access-Control-Allow-Origin', value: 'https://alteroffice1.netlify.app' },
1819
{ key: 'Access-Control-Allow-Methods', value: 'GET, POST, PUT, DELETE, OPTIONS' },
1920
{ key: 'Access-Control-Allow-Headers', value: 'Content-Type, Authorization, X-API-Key' },

src/middleware.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,26 @@ export async function middleware(request: NextRequest) {
77
return NextResponse.next();
88
}
99

10+
// Get the origin from the request
11+
const origin = request.headers.get('origin') || '';
12+
13+
// Allow specific domains
14+
const allowedOrigins = [
15+
'https://alteroffice1.netlify.app',
16+
'https://alter-office-pi.vercel.app',
17+
'http://localhost:3000',
18+
'http://localhost:3001',
19+
'http://localhost:3002'
20+
];
21+
22+
const isAllowedOrigin = allowedOrigins.includes(origin) || origin.includes('localhost') || origin.includes('netlify') || origin.includes('vercel');
23+
1024
// Handle preflight requests
1125
if (request.method === 'OPTIONS') {
1226
return new NextResponse(null, {
1327
status: 200,
1428
headers: {
15-
'Access-Control-Allow-Origin': 'https://alteroffice1.netlify.app',
29+
'Access-Control-Allow-Origin': isAllowedOrigin ? origin : 'https://alteroffice1.netlify.app',
1630
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
1731
'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-API-Key',
1832
'Access-Control-Max-Age': '86400',

0 commit comments

Comments
 (0)