Skip to content

Commit

Permalink
Fix CORS for narrative.tech and localhost
Browse files Browse the repository at this point in the history
  • Loading branch information
samhatoum committed Nov 26, 2024
1 parent 932737e commit 81d3ce4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import './loadEnv';
import 'reflect-metadata';
import {
EntityManager,
EntityRepository,
MikroORM,
RequestContext,
} from '@mikro-orm/core';
import {EntityManager, EntityRepository, MikroORM, RequestContext,} from '@mikro-orm/core';
import cors from 'cors';
import express from 'express';
import path from 'path';
Expand Down Expand Up @@ -85,7 +80,7 @@ const initializeApp = async () => {
const mikroOrmConfig = {
...(await import(
`./mikro-orm.${process.env.MIKRO_ORM_DRIVER || 'sqlite'}${isTypescript ? '.ts' : '.js'}`
).then((module) => module.default)),
).then((module) => module.default)),
};

DI.orm = await MikroORM.init(mikroOrmConfig);
Expand Down Expand Up @@ -120,7 +115,12 @@ const initializeApp = async () => {
app.use(express.urlencoded({limit: '50mb', extended: true}));
app.use(
cors({
origin: [getWebsiteDomain()],
origin: (origin, callback) => {
const allowedOrigins = [getWebsiteDomain()];
const regex = /^(https:\/\/[a-zA-Z0-9-]+\.narrative\.tech|https?:\/\/localhost(:\d+)?)$/;
if (!origin || allowedOrigins.includes(origin) || regex.test(origin)) callback(null, true);
else callback(new Error('Not allowed by CORS'));
},
allowedHeaders: [
'content-type',
...supertokens.getAllCORSHeaders(),
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 81d3ce4

Please sign in to comment.