File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -13,16 +13,26 @@ interface AppDeps {
1313 sessionController : SessionController ;
1414 authController ?: LocalAuthController ;
1515 authService ?: LocalAuthService ;
16- corsOrigin ?: string | string [ ] | boolean ;
16+ corsOrigin ?:
17+ | string
18+ | string [ ]
19+ | boolean
20+ | ( ( origin : string | undefined , callback : ( err : Error | null , allow ?: boolean ) => void ) => void ) ;
1721 env ?: AppEnv ;
1822}
1923
2024export function createApp ( deps : AppDeps ) {
2125 const app = express ( ) ;
2226
2327 // CORS configuration
24- // Note: When credentials: true, origin cannot be '*' - must be explicit
25- const corsOrigin = deps . corsOrigin ?? true ; // 'true' reflects the request origin
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+ } ;
2636
2737 app . use (
2838 cors ( {
Original file line number Diff line number Diff line change @@ -20,7 +20,10 @@ const EnvSchema = z.object({
2020} ) ;
2121
2222export type AppEnv = z . infer < typeof EnvSchema > & {
23- corsOrigin : string | string [ ] | boolean ;
23+ corsOrigin :
24+ | string
25+ | string [ ]
26+ | ( ( origin : string | undefined , callback : ( err : Error | null , allow ?: boolean ) => void ) => void ) ;
2427} ;
2528
2629export function loadEnv ( ) : AppEnv {
@@ -30,12 +33,14 @@ export function loadEnv(): AppEnv {
3033 }
3134
3235 // Parse CORS_ORIGIN - can be a comma-separated list or single origin
33- // If not set, use 'true' to reflect the request origin ( allows all with credentials)
36+ // If not set, use a function that allows all origins (for credentials support )
3437 const corsOrigin = parsed . data . CORS_ORIGIN
3538 ? parsed . data . CORS_ORIGIN . includes ( ',' )
3639 ? parsed . data . CORS_ORIGIN . split ( ',' ) . map ( ( origin ) => origin . trim ( ) )
3740 : parsed . data . CORS_ORIGIN
38- : true ;
41+ : ( ( _origin : string | undefined , callback : ( err : Error | null , allow ?: boolean ) => void ) => {
42+ callback ( null , true ) ;
43+ } ) ;
3944
4045 return {
4146 ...parsed . data ,
Original file line number Diff line number Diff line change @@ -41,6 +41,15 @@ async function bootstrap() {
4141 env,
4242 } ) ;
4343
44+ // Log CORS configuration
45+ if ( typeof env . corsOrigin === 'string' ) {
46+ console . log ( `CORS enabled for origin: ${ env . corsOrigin } ` ) ;
47+ } else if ( Array . isArray ( env . corsOrigin ) ) {
48+ console . log ( `CORS enabled for origins: ${ env . corsOrigin . join ( ', ' ) } ` ) ;
49+ } else {
50+ console . log ( 'CORS enabled for all origins (dynamic reflection)' ) ;
51+ }
52+
4453 app . listen ( env . PORT , ( ) => {
4554 console . log ( `Auralyze API listening on port ${ env . PORT } ` ) ;
4655 } ) ;
You can’t perform that action at this time.
0 commit comments