Skip to content

Commit 6626734

Browse files
feat: implement regexp for cors for evailability netlify deploys (#17)
1 parent 9a775b0 commit 6626734

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

backend/src/main.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import compression from 'compression';
77
import cookieParser from 'cookie-parser';
88

99
import { AppModule } from './app/app.module';
10-
import { FRONTEND_URL, PORT } from './config/constants';
10+
import { PORT } from './config/constants';
1111
import { DEFAULT_PORT } from './constants';
1212

1313
async function bootstrap() {
@@ -19,13 +19,34 @@ async function bootstrap() {
1919
const globalPrefix = 'api/v1';
2020
app.setGlobalPrefix(globalPrefix);
2121

22-
const origin = configService?.get<string>(FRONTEND_URL);
23-
2422
app.use(cookieParser());
2523
app.use(helmet());
26-
// TODO: we need to set frontend url and update it in railway
2724
app.enableCors({
28-
origin: [origin || '*', 'http://localhost:3001', 'http://localhost:5173'],
25+
origin: (origin, callback) => {
26+
if (!origin) {
27+
return callback(null, true);
28+
}
29+
30+
const allowedOrigins = [
31+
'http://localhost:5173',
32+
'http://localhost:3001',
33+
'https://fit-tracker-corp.netlify.app',
34+
];
35+
36+
if (allowedOrigins.includes(origin)) {
37+
return callback(null, true);
38+
}
39+
40+
const isNetlifyPreview =
41+
/^https:\/\/deploy-preview-\d+--fit-tracker-corp\.netlify\.app$/.test(
42+
origin,
43+
);
44+
45+
if (isNetlifyPreview) {
46+
return callback(null, true);
47+
}
48+
callback(new Error('Not allowed by CORS'));
49+
},
2950
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
3051
credentials: true,
3152
});

0 commit comments

Comments
 (0)