CORS implemented in a static manner: unable to configure on another machine

This commit is contained in:
aodulov
2025-10-15 13:45:36 +03:00
parent ea0e025b0e
commit 03d31011fd
18 changed files with 582 additions and 16 deletions

View File

@@ -20,7 +20,21 @@ const server = http.createServer(app);
// Middleware
app.use(express.json());
app.use(cors());
const allowedOrigins = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
const corsOptions = {
origin: (origin: string | undefined, callback: (err: Error | null, allow?: boolean) => void) => {
// Allow same-origin requests (origin is undefined) and requests from the whitelisted origins
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
console.warn(`CORS: Blocked request from origin: ${origin}`);
callback(new Error('Not allowed by CORS'));
}
},
};
app.use(cors(corsOptions));
// Public API Routes
app.use('/api/auth', authRouter);