Skip to content

Commit

Permalink
fix(server): add CORS (#932)
Browse files Browse the repository at this point in the history
* fix(server): add CORS

* style: fix typo

---------

Co-authored-by: WikiRik <WikiRik@users.noreply.github.com>
  • Loading branch information
WikiRik and WikiRik authored May 2, 2023
1 parent d54e2c2 commit 7bd1240
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const express = require('express');
const cors = require('cors');
const router = require('express-promise-router');
const bodyParser = require('body-parser');
const boolParser = require('express-query-boolean');
Expand Down Expand Up @@ -32,14 +33,18 @@ process.on('unhandledRejection', (err) => {
}
});

const corsOptions = {
origin: [/aegee\.eu|aegee\.org|app\.aegee-leiden\.nl/]
};

GeneralRouter.get('/healthcheck', middlewares.healthcheck);
GeneralRouter.get('/metrics', metrics.getMetrics);
GeneralRouter.get('/metrics/requests', endpointsMetrics.getEndpointMetrics);

// For all the requests above these three, query the core for authorization data.
GeneralRouter.use(middlewares.authenticateUser);

GeneralRouter.get('/', events.listEvents);
GeneralRouter.get('/', cors(corsOptions), events.listEvents);
GeneralRouter.post('/', middlewares.ensureAuthorized, events.addEvent);

GeneralRouter.get('/mine/organizing', middlewares.ensureAuthorized, events.listUserOrganizedEvents);
Expand All @@ -51,7 +56,7 @@ GeneralRouter.get('/boardview/:body_id', middlewares.ensureAuthorized, events.li
EventsRouter.use(middlewares.fetchSingleEvent);

// Getting the event details can be done without autorization.
EventsRouter.get('/', events.eventDetails);
EventsRouter.get('/', cors(corsOptions), events.eventDetails);

// The next routes cannot.
EventsRouter.use(middlewares.ensureAuthorized);
Expand Down
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@bugsnag/js": "^7.18.0",
"body-parser": "^1.19.0",
"bunyan": "^1.8.15",
"cors": "^2.8.5",
"express": "^4.18.2",
"express-promise-router": "^4.1.1",
"express-query-boolean": "^2.0.0",
Expand Down

0 comments on commit 7bd1240

Please sign in to comment.