Skip to content

Commit 2d36288

Browse files
committed
feat: simplify CORS origin handling and update environment variable type
1 parent 2bed66f commit 2d36288

2 files changed

Lines changed: 4 additions & 24 deletions

File tree

src/app.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,16 @@ interface AppDeps {
1313
sessionController: SessionController;
1414
authController?: LocalAuthController;
1515
authService?: LocalAuthService;
16-
corsOrigin?:
17-
| string
18-
| string[]
19-
| boolean
20-
| ((origin: string | undefined, callback: (err: Error | null, allow?: boolean) => void) => void);
16+
corsOrigin?: string | string[];
2117
env?: AppEnv;
2218
}
2319

2420
export function createApp(deps: AppDeps) {
2521
const app = express();
2622

27-
// CORS configuration
28-
// When credentials: true, origin cannot be '*' - must be explicit or dynamic
29-
// Use a function to dynamically reflect the requesting origin
30-
const corsOrigin = deps.corsOrigin
31-
? deps.corsOrigin
32-
: (origin: string | undefined, callback: (err: Error | null, allow?: boolean) => void) => {
33-
// Reflect any origin when CORS_ORIGIN not set
34-
callback(null, true);
35-
};
36-
3723
app.use(
3824
cors({
39-
origin: corsOrigin,
25+
origin: deps.corsOrigin ?? '*',
4026
credentials: true,
4127
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
4228
allowedHeaders: ['Content-Type', 'Authorization', 'Cookie'],

src/config/env.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ const EnvSchema = z.object({
2020
});
2121

2222
export type AppEnv = z.infer<typeof EnvSchema> & {
23-
corsOrigin:
24-
| string
25-
| string[]
26-
| ((origin: string | undefined, callback: (err: Error | null, allow?: boolean) => void) => void);
23+
corsOrigin: string | string[];
2724
};
2825

2926
export function loadEnv(): AppEnv {
@@ -33,14 +30,11 @@ export function loadEnv(): AppEnv {
3330
}
3431

3532
// Parse CORS_ORIGIN - can be a comma-separated list or single origin
36-
// If not set, use a function that allows all origins (for credentials support)
3733
const corsOrigin = parsed.data.CORS_ORIGIN
3834
? parsed.data.CORS_ORIGIN.includes(',')
3935
? parsed.data.CORS_ORIGIN.split(',').map((origin) => origin.trim())
4036
: parsed.data.CORS_ORIGIN
41-
: ((_origin: string | undefined, callback: (err: Error | null, allow?: boolean) => void) => {
42-
callback(null, true);
43-
});
37+
: '*';
4438

4539
return {
4640
...parsed.data,

0 commit comments

Comments
 (0)