From 491956d84bea0179c7b2c6572785591a6c512ceb Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 31 Dec 2024 14:12:44 +0100 Subject: [PATCH] cors config --- backend/src/server.ts | 24 +++++++------------ .../src/components/socketio/SocketService.ts | 4 ++-- frontend/src/config-front.js | 8 ++++--- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/backend/src/server.ts b/backend/src/server.ts index c8f390dd..e1305c3a 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -15,27 +15,19 @@ import appRouter from './routes'; const app = express(); const server = http.createServer(app); -// TODO nicolas finetune CORS config... +const corsOptions = { + origins: '*', + methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], + allowedHeaders: ['Content-Type', 'Authorization', 'x-socket-id'], + credentials: true, +}; + const io = new Server(server, { - cors: { - origin: '*', // Allow all origins - methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], // Allowed HTTP methods - allowedHeaders: ['x-socket-id'], // Optional: specify allowed headers - credentials: true, // Allow credentials (e.g., cookies) - }, + cors: corsOptions }); sgMail.setApiKey(appConfig.SENDGRID_API_KEY); -// TODO nicolas finetune CORS config... -// Allow only specific origins (e.g., your frontend's URL) -const corsOptions = { - origin: 'http://localhost:3000', // Replace with your frontend URL - methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], // Allowed HTTP methods - allowedHeaders: ['Content-Type', 'Authorization', 'x-socket-id'], // Allowed headers - credentials: true, // Allow cookies and credentials -}; - app.use(cors(corsOptions)); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: true})); diff --git a/frontend/src/components/socketio/SocketService.ts b/frontend/src/components/socketio/SocketService.ts index 19a6bf9f..d5b5a9b0 100644 --- a/frontend/src/components/socketio/SocketService.ts +++ b/frontend/src/components/socketio/SocketService.ts @@ -7,10 +7,10 @@ import { EntityEventPayload } from "./EntityEventPayload"; import { t } from "../utils"; import { toast } from "react-toastify"; import { handleRoleSocketEvents, handleUserSocketEvents } from "../../actions/userActions"; +import { baseUrl } from "../../config-front"; function createSocketService () { - // TODO nicolas read server url from frontend config !!! - const socket = io('localhost:9000'); + const socket = io(baseUrl); var socketId: undefined|string = undefined; var initialized = false; diff --git a/frontend/src/config-front.js b/frontend/src/config-front.js index 56802166..92b44e00 100644 --- a/frontend/src/config-front.js +++ b/frontend/src/config-front.js @@ -1,7 +1,9 @@ if (process.env.NODE_ENV === 'production') { console.log('process.env', process.env); - const backend = `${window.location.origin}/api`; - module.exports = {backend}; + const baseUrl = window.location.origin; + const backend = `${baseUrl}/api`; + module.exports = {backend, baseUrl}; } else { - module.exports = {backend: 'http://localhost:9000/api'}; + const baseUrl = 'http://localhost:9000'; + module.exports = {backend: baseUrl+'/api',baseUrl}; }