Skip to content

Commit

Permalink
cors config
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasHub committed Dec 31, 2024
1 parent c28bc71 commit 491956d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
24 changes: 8 additions & 16 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}));
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/socketio/SocketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions frontend/src/config-front.js
Original file line number Diff line number Diff line change
@@ -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};
}

0 comments on commit 491956d

Please sign in to comment.