Skip to content

Commit d86201c

Browse files
priyanshu ojhapriyanshu ojha
authored andcommitted
fix: add Vercel origins to CORS + explicit OPTIONS preflight handler
1 parent 39ff00e commit d86201c

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

backend/server.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,35 @@ const allowedOrigins = [
2525
'http://127.0.0.1:5175',
2626
'https://codeaurelius0.github.io',
2727
'https://codeaurelius0.github.io/stegokit',
28+
// Vercel deployments
29+
'https://stegokit.vercel.app',
2830
].filter(Boolean);
31+
32+
// Also allow any *.vercel.app preview URL and anything in CORS_ORIGIN
33+
function isOriginAllowed(origin) {
34+
if (!origin) return true; // server-to-server / curl
35+
if (allowedOrigins.includes(origin)) return true;
36+
// Allow all Vercel preview deployments
37+
if (/^https:\/\/stegokit[a-z0-9-]*\.vercel\.app$/.test(origin)) return true;
38+
return false;
39+
}
2940
const frontendDist = path.resolve(__dirname, '../frontend/dist');
3041

3142
// ── Middleware ────────────────────────────────────────────
3243
app.use(cors({
3344
origin: (origin, callback) => {
34-
if (!origin || allowedOrigins.includes(origin)) {
45+
if (isOriginAllowed(origin)) {
3546
callback(null, true);
36-
return;
47+
} else {
48+
callback(new Error(`CORS origin not allowed: ${origin}`));
3749
}
38-
callback(new Error('CORS origin not allowed'));
3950
},
4051
methods: ['GET', 'POST', 'OPTIONS'],
4152
allowedHeaders: ['Content-Type', 'Authorization'],
53+
credentials: false,
4254
}));
55+
// Ensure OPTIONS preflight is handled for all routes
56+
app.options('*', cors());
4357

4458
app.use(express.json({ limit: '500mb' }));
4559
app.use(express.urlencoded({ extended: true, limit: '500mb' }));

0 commit comments

Comments
 (0)