Skip to content

Commit e1c07b3

Browse files
Fix CORS: Allow AWS Amplify frontend domain
1 parent 6388741 commit e1c07b3

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

backend/Dockerfile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ COPY package*.json ./
88
# Install dependencies
99
RUN npm install
1010

11-
# Copy source code
12-
COPY . .
11+
# Copy TypeScript config and source
12+
COPY tsconfig.json ./
13+
COPY src ./src
1314

1415
# Expose port
1516
EXPOSE 4000
1617

17-
# Start the application
18-
CMD ["npx", "ts-node-dev", "src/index.ts"]
18+
# Start application with ts-node
19+
CMD ["npx", "ts-node", "src/index.ts"]

backend/src/index.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,35 @@ import { authMiddleware } from './middleware/auth';
1515
dotenv.config();
1616

1717
const app = express();
18+
19+
// Allow multiple origins for CORS
20+
const allowedOrigins = [
21+
'http://localhost:5173',
22+
'https://main.d3gxu1z7qiv7tn.amplifyapp.com', // Your AWS Amplify frontend
23+
/\.amplifyapp\.com$/ // Allow any Amplify domain
24+
];
25+
1826
app.use(cors({
19-
origin: 'http://localhost:5173',
27+
origin: (origin, callback) => {
28+
// Allow requests with no origin (like mobile apps or curl)
29+
if (!origin) return callback(null, true);
30+
31+
// Check if origin is in allowed list or matches pattern
32+
const isAllowed = allowedOrigins.some(allowed => {
33+
if (typeof allowed === 'string') return allowed === origin;
34+
if (allowed instanceof RegExp) return allowed.test(origin);
35+
return false;
36+
});
37+
38+
if (isAllowed) {
39+
callback(null, true);
40+
} else {
41+
callback(new Error('Not allowed by CORS'));
42+
}
43+
},
2044
credentials: true,
2145
}));
46+
2247
app.use(express.json());
2348
app.use(authMiddleware);
2449

@@ -36,10 +61,7 @@ async function startServer() {
3661
server.applyMiddleware({
3762
app,
3863
path: '/graphql',
39-
cors: {
40-
origin: 'http://localhost:5173',
41-
credentials: true,
42-
}
64+
cors: false // Disable Apollo's CORS, use Express CORS instead
4365
});
4466

4567
const PORT = process.env.PORT || 4000;

0 commit comments

Comments
 (0)