- Update backend main.ts to include www.placebo.mk in allowed origins - Add logging to show which origins are allowed - Set FRONTEND_URL, PWA_URL, STRAPI_URL env vars in docker-compose - This fixes 'No Access-Control-Allow-Origin header' CORS errors
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import { NestFactory, Reflector } from '@nestjs/core';
|
|
import { ValidationPipe } from '@nestjs/common';
|
|
import { AppModule } from './app.module';
|
|
import { JwtAuthPublicGuard } from './modules/auth/jwt-auth-public.guard';
|
|
|
|
async function bootstrap() {
|
|
const app = await NestFactory.create(AppModule);
|
|
|
|
// Build allowed origins list from environment variables
|
|
const allowedOrigins = [
|
|
process.env.FRONTEND_URL ?? 'http://localhost:5173',
|
|
'https://www.placebo.mk', // Also allow www subdomain
|
|
process.env.PWA_URL ?? 'http://localhost:5174',
|
|
process.env.STRAPI_URL ?? 'http://localhost:1337',
|
|
].filter(Boolean); // Remove any undefined/null values
|
|
|
|
console.log('CORS enabled for origins:', allowedOrigins);
|
|
|
|
app.enableCors({
|
|
origin: allowedOrigins,
|
|
credentials: true,
|
|
});
|
|
|
|
app.setGlobalPrefix('api/v1');
|
|
|
|
// Apply global validation pipe
|
|
app.useGlobalPipes(
|
|
new ValidationPipe({
|
|
whitelist: true,
|
|
transform: true,
|
|
forbidNonWhitelisted: true,
|
|
}),
|
|
);
|
|
|
|
// Apply global authentication guard
|
|
const reflector = app.get(Reflector);
|
|
app.useGlobalGuards(new JwtAuthPublicGuard(reflector));
|
|
|
|
const port = process.env.PORT ?? 3000;
|
|
const host = '0.0.0.0'; // Bind to all interfaces for Docker
|
|
await app.listen(port, host);
|
|
|
|
console.log(`Application is running on: http://${host}:${port}`);
|
|
}
|
|
void bootstrap();
|