Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import compression from 'compression';
import cookieParser from 'cookie-parser';

import { AppModule } from './app/app.module';
import { FRONTEND_URL, PORT } from './config/constants';
import { PORT } from './config/constants';
import { DEFAULT_PORT } from './constants';

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

const origin = configService?.get<string>(FRONTEND_URL);

app.use(cookieParser());
app.use(helmet());
// TODO: we need to set frontend url and update it in railway
app.enableCors({
origin: [origin || '*', 'http://localhost:3001', 'http://localhost:5173'],
origin: (origin, callback) => {
if (!origin) {
return callback(null, true);
}

const allowedOrigins = [
'http://localhost:5173',
'http://localhost:3001',
'https://fit-tracker-corp.netlify.app',
];

if (allowedOrigins.includes(origin)) {
return callback(null, true);
}

const isNetlifyPreview =
/^https:\/\/deploy-preview-\d+--fit-tracker-corp\.netlify\.app$/.test(
origin,
);

if (isNetlifyPreview) {
return callback(null, true);
}
callback(new Error('Not allowed by CORS'));
},
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
credentials: true,
});
Expand Down