const allowedOrigins = [
// "http://localhost:3000",
"https://neuera.in",
"https://www.neuera.in",
];
const corsOptions = {
origin: (origin: string | undefined, callback: (err: Error | null, allow?: boolean) => void) => {
// Allow requests with no origin (mobile apps, Postman, server-to-server)
if (!origin) {
console.log("✅ No origin header - allowing request");
return callback(null, true);
}
if (allowedOrigins.includes(origin)) {
console.log(`✅ CORS allowed for origin: ${origin}`);
callback(null, true);
} else {
console.warn(`❌ CORS blocked origin: ${origin}`);
callback(new Error(`CORS policy violation: Origin ${origin} not allowed`));
}
},
credentials: true,
optionsSuccessStatus: 200,
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
allowedHeaders: [
'Origin',
'X-Requested-With',
'Content-Type',
'Accept',
'Authorization',
'Cache-Control',
'X-API-Key',
'Access-Control-Allow-Headers',
'Access-Control-Allow-Origin'
],
// Explicitly set headers for debugging
preflightContinue: false
};
// Apply CORS BEFORE other middleware
app.use(cors(corsOptions));
// Security headers (configure after CORS)
app.use(helmet({
crossOriginResourcePolicy: { policy: "cross-origin" },
// Disable some helmet defaults that might interfere
contentSecurityPolicy: false,
crossOriginEmbedderPolicy: false
}));
frontend:-rtqk
export const api = createApi({
baseQuery: customBaseQuery,
reducerPath: "api",
tagTypes: ["Projects", "Tasks", "Users", "Teams"],
endpoints: (build) => ({
getAuthUser: build.query({
queryFn: async (_, _queryApi, _extraoptions, fetchWithBQ) => {
try {
const user = await getCurrentUser();
const session = await fetchAuthSession();
if (!session) {
// Format errors according to RTK Query expectations
return {
error: {
status: 401,
data: 'No session found'
} as FetchBaseQueryError,
};
}
const { userSub } = session;
// const { accessToken } = session.tokens ?? {};
const userDetailsResponse = await fetchWithBQ(`users/${userSub}`);
const userDetails = userDetailsResponse.data as User;
if (userDetailsResponse.error) {
return { error: userDetailsResponse.error };
}
return { data: { user, userSub, userDetails } };
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : "Could not fetch user data";
return {
error: {
status: 'CUSTOM_ERROR',
data: errorMessage
} as FetchBaseQueryError,
};
}
},
}),
im trying to host my frontend on vercel(nextjs) and backend(node+ex) on aws ec2 but im keept getting this cors err , i have done this but none if this works any idea , i have created api.neuera.in for api calls but still nothing working,
Do I need to add something to the EC2 instance like a proxy or Nginx rule, even though i hvae nginx setup on ec2?
i want to connnect my frontend and backend?