From 02d3050ca0257682cba840eb4180ff24db8edb19 Mon Sep 17 00:00:00 2001 From: Devesh Shukla Date: Sun, 25 Aug 2024 17:20:59 +0530 Subject: [PATCH] rituvercelLink --- backend/src/server.js | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/backend/src/server.js b/backend/src/server.js index c510e76..314fb30 100644 --- a/backend/src/server.js +++ b/backend/src/server.js @@ -15,21 +15,29 @@ dotenv.config({ }) const app = express() const port = 3000 -const dep = (origin, callback) => { - const allowedOrigins = [ - 'https://www.bequiet.live', - 'https://www.bequiet.vercel.app' - ]; +const allowedOrigins = [ + 'https://www.bequiet.live', + 'https://www.bequiet.vercel.app' +]; - if (allowedOrigins.includes(origin) || !origin) { - callback(null, true); - } else { - callback(new Error('Not allowed by CORS')); - } -}; const corsOptions = { credentials: true, - origin: (process.env.NODE_ENV === 'development')?'http://localhost:5173':dep, + origin: (origin, callback) => { + if (process.env.NODE_ENV === 'development') { + if (origin === 'http://localhost:5173') { + callback(null, true); + } else { + callback(new Error('Not allowed by CORS')); + } + } else { + // Allow only specified origins in production + if (allowedOrigins.includes(origin) || !origin) { + callback(null, true); + } else { + callback(new Error('Not allowed by CORS')); + } + } + }, methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], allowedHeaders: [ "Origin",