fixed cors

This commit is contained in:
moulichand16
2026-03-27 16:05:18 +05:30
parent e912b982df
commit 09c7930b52
6 changed files with 14 additions and 43 deletions

View File

@@ -1,6 +1,6 @@
# Server
PORT=4100
CORS_ORIGIN=http://localhost:5173
CORS_ORIGINS=http://localhost:5173,http://localhost:8000
# Fortytwo Platform
PLATFORM_GRAPHQL_URL=http://localhost:4000/graphql

View File

@@ -1,6 +1,9 @@
export default () => ({
port: parseInt(process.env.PORT ?? '4100', 10),
corsOrigin: process.env.CORS_ORIGIN ?? 'http://localhost:5173',
corsOrigins: (process.env.CORS_ORIGINS ?? 'http://localhost:5173')
.split(',')
.map(origin => origin.trim())
.filter(origin => origin.length > 0),
platform: {
graphqlUrl: process.env.PLATFORM_GRAPHQL_URL ?? 'http://localhost:4000/graphql',
apiKey: process.env.PLATFORM_API_KEY ?? '',

View File

@@ -1,27 +0,0 @@
import { Injectable, NestMiddleware, Logger } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
@Injectable()
export class EmbedCorsMiddleware implements NestMiddleware {
private readonly logger = new Logger(EmbedCorsMiddleware.name);
use(req: Request, res: Response, next: NextFunction) {
const origin = req.headers.origin || '*';
this.logger.debug(`Embed CORS middleware - ${req.method} ${req.path} from ${origin}`);
// Set CORS headers for all requests
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization');
res.setHeader('Access-Control-Max-Age', '86400'); // 24 hours
// Handle preflight requests
if (req.method === 'OPTIONS') {
this.logger.debug('Handling OPTIONS preflight request');
res.status(204).end();
return;
}
next();
}
}

View File

@@ -1,18 +1,9 @@
import { Module, NestModule, MiddlewareConsumer, RequestMethod } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { PlatformModule } from '../platform/platform.module';
import { LeadEmbedController } from './lead-embed.controller';
import { EmbedCorsMiddleware } from './embed-cors.middleware';
@Module({
imports: [PlatformModule],
controllers: [LeadEmbedController],
})
export class EmbedModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(EmbedCorsMiddleware)
.forRoutes(
{ path: 'embed/*', method: RequestMethod.ALL }
);
}
}
export class EmbedModule {}

View File

@@ -16,6 +16,7 @@ export class LeadEmbedController {
@Post('create')
async handleLeadCreation(@Body() body: Record<string, any>) {
console.log("Lead creation from embed received:", body);
this.logger.log(`Lead creation from embed received: ${JSON.stringify(body)}`);
const authHeader = this.apiKey ? `Bearer ${this.apiKey}` : '';
@@ -138,7 +139,7 @@ export class LeadEmbedController {
try {
const activityType = body.type === 'consultation' || body.type === 'appointment'
? 'APPOINTMENT_BOOKED'
: 'FORM_SUBMITTED';
: 'CALL_RECEIVED';
let summary = 'Lead submitted via web form';
if (body.type) {

View File

@@ -6,11 +6,14 @@ async function bootstrap() {
const app = await NestFactory.create(AppModule);
const config = app.get(ConfigService);
app.enableCors({
origin: config.get('corsOrigin'),
credentials: true,
});
const corsOrigins = config.get<string[]>('corsOrigins') || ['http://localhost:5173'];
app.enableCors({
origin: corsOrigins,
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Accept', 'Authorization'],
});
const port = config.get('port');
await app.listen(port);