Skip to content

Commit c7e94f3

Browse files
feat: add env-based CORS origin support
2 parents aa1718f + 5a75a9c commit c7e94f3

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

package-lock.json

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"dependencies": {
4848
"@prisma/client": "^6.6.0",
4949
"bcryptjs": "^3.0.2",
50+
"cors": "^2.8.5",
5051
"express": "^5.1.0",
5152
"helmet": "^8.1.0",
5253
"jsonwebtoken": "^9.0.2",
@@ -59,6 +60,7 @@
5960
"devDependencies": {
6061
"@eslint/js": "^9.24.0",
6162
"@stylistic/eslint-plugin": "^4.2.0",
63+
"@types/cors": "^2.8.17",
6264
"@types/eslint__js": "^8.42.3",
6365
"@types/eslint-plugin-security": "^3.0.0",
6466
"@types/express": "^5.0.1",

src/app.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import requestLogger from './middlewares/request-logger';
22
import errorHandler from './middlewares/error-handler';
3+
import { ALLOWED_ORIGINS } from './lib/config';
34
import AppError from './lib/app-error';
45
import apiV1Router from './api/v1';
56
import express from 'express';
67
import helmet from 'helmet';
8+
import cors from 'cors';
9+
import logger from './lib/logger';
710

811
const app = express();
912

@@ -13,6 +16,14 @@ app.use(helmet());
1316
app.use(express.json());
1417
app.use(requestLogger);
1518

19+
logger.info('ALLOWED_ORIGINS: ', ALLOWED_ORIGINS);
20+
app.use(
21+
cors({
22+
origin: ALLOWED_ORIGINS,
23+
optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
24+
})
25+
);
26+
1627
app.use('/api/v1', apiV1Router);
1728

1829
app.use((req) => {

src/lib/config.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
if (!process.env.NODE_ENV) console.warn('Miss Env Var: NODE_ENV');
22
if (!process.env.SECRET) console.error('Missing Env Var: SECRET');
33
if (!process.env.ADMIN_SECRET) console.error('Missing Env Var: ADMIN_SECRET');
4+
if (!process.env.ALLOWED_ORIGINS)
5+
console.log('Missing Env Var: ALLOWED_ORIGINS');
6+
7+
export const ALLOWED_ORIGINS = process.env.ALLOWED_ORIGINS
8+
? process.env.ALLOWED_ORIGINS.split(',').map((origin) => origin.trim())
9+
: [];
410

511
export const SALT = (Number(process.env.SALT) || process.env.SALT) ?? 10;
612
export const SECRET = process.env.SECRET ?? 'secret';
@@ -9,4 +15,12 @@ export const TOKEN_EXP_PERIOD = process.env.TOKEN_EXP_PERIOD ?? '3d';
915
export const NODE_ENV = process.env.NODE_ENV;
1016
export const CI = Boolean(process.env.CI);
1117

12-
export default { SALT, SECRET, ADMIN_SECRET, TOKEN_EXP_PERIOD, NODE_ENV, CI };
18+
export default {
19+
TOKEN_EXP_PERIOD,
20+
ALLOWED_ORIGINS,
21+
ADMIN_SECRET,
22+
NODE_ENV,
23+
SECRET,
24+
SALT,
25+
CI,
26+
};

0 commit comments

Comments
 (0)