Commit 80147bab authored by AravindR-K's avatar AravindR-K

fix: harden CORS to allow Vercel deployments

parent f446c644
...@@ -13,21 +13,36 @@ connectDB(); ...@@ -13,21 +13,36 @@ connectDB();
const app = express(); const app = express();
// Middleware // Middleware
app.use(cors({ const allowedOrigins = [
origin: function (origin, callback) {
const allowed = [
'http://localhost:4200', 'http://localhost:4200',
'http://localhost:3000',
process.env.FRONTEND_URL process.env.FRONTEND_URL
]; ].filter(Boolean);
// Allow any origin that ends with 'vercel.app' or is directly allowed
if (!origin || allowed.includes(origin) || origin.endsWith('vercel.app')) { app.use(cors({
callback(null, true); origin: function (origin, callback) {
} else { // Allow requests with no origin (mobile apps, curl, Postman, server-to-server)
callback(null, false); if (!origin) return callback(null, true);
// Allow any vercel.app previews and production deployments
if (origin.endsWith('.vercel.app') || origin.includes('vercel.app')) {
return callback(null, true);
}
// Allow explicitly listed origins
if (allowedOrigins.includes(origin)) {
return callback(null, true);
} }
// Block everything else
console.warn(`CORS blocked origin: ${origin}`);
return callback(new Error(`CORS policy: origin ${origin} not allowed`), false);
}, },
credentials: true credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization']
})); }));
app.use(express.json()); app.use(express.json());
app.use(express.urlencoded({ extended: true })); app.use(express.urlencoded({ extended: true }));
app.use(cookieParser()); app.use(cookieParser());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment