Skip to content

Commit 52eace0

Browse files
committed
feat: implement CORS support with configurable origins
1 parent 7d7d163 commit 52eace0

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

.example.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ LOG_LEVEL=info
1616
API_PORT=1234
1717
API_PREFIX=/api
1818
API_VERSION=/v2
19+
API_ALLOWED_CORS_ORIGINS=http://localhost:3000,https://api.podverse.fm,https://podverse.fm
1920

2021
AUTH_JWT_SECRET=your-secret-whatever
2122

package-lock.json

Lines changed: 28 additions & 3 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
@@ -35,6 +35,7 @@
3535
"bcrypt": "^5.1.1",
3636
"body-parser": "^1.20.2",
3737
"cookie-parser": "^1.4.7",
38+
"cors": "^2.8.5",
3839
"express": "^4.19.2",
3940
"joi": "^17.13.3",
4041
"jsonwebtoken": "^9.0.2",
@@ -57,6 +58,7 @@
5758
"@eslint/js": "^9.8.0",
5859
"@types/bcrypt": "^5.0.2",
5960
"@types/cookie-parser": "^1.4.8",
61+
"@types/cors": "^2.8.19",
6062
"@types/express": "^4.17.21",
6163
"@types/jest": "^29.5.14",
6264
"@types/node": "^22.0.0",

src/app.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import "reflect-metadata";
22
import bodyParser from 'body-parser';
33
import cookieParser from 'cookie-parser';
4+
import cors from 'cors';
45
import express, { NextFunction, Request, Response } from "express";
56
import { CategoryService } from "podverse-orm";
67
import { config } from '@api/config';
@@ -23,6 +24,11 @@ import { loggerService } from "./factories/loggerService";
2324
export const app = express();
2425
const port = 1234;
2526

27+
app.use(cors({
28+
origin: config.api.allowedCORSOrigins,
29+
credentials: true
30+
}));
31+
2632
app.use(bodyParser.json());
2733
app.use(bodyParser.urlencoded({ extended: true }));
2834

src/config/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Config = {
2222
domain: string;
2323
secure: boolean;
2424
};
25+
allowedCORSOrigins: string[];
2526
};
2627
email: {
2728
styles: {
@@ -90,6 +91,7 @@ export const config: Config = {
9091
domain: process.env.COOKIE_DOMAIN || 'localhost',
9192
secure: process.env.COOKIE_SECURE === 'true',
9293
},
94+
allowedCORSOrigins: (process.env.API_ALLOWED_CORS_ORIGINS || '').split(',').map(origin => origin.trim()),
9395
},
9496
email: {
9597
styles: {

0 commit comments

Comments
 (0)