@@ -17,6 +17,7 @@ import {
1717} from '../../../express.tokens' ;
1818import { isNotEmpty } from '../../shared/empty.util' ;
1919import { HardRedirectService } from './hard-redirect.service' ;
20+ import { ServerResponseService } from './server-response.service' ;
2021
2122/**
2223 * Service for performing hard redirects within the server app module
@@ -28,6 +29,7 @@ export class ServerHardRedirectService extends HardRedirectService {
2829 @Inject ( APP_CONFIG ) protected appConfig : AppConfig ,
2930 @Inject ( REQUEST ) protected req : Request ,
3031 @Inject ( RESPONSE ) protected res : Response ,
32+ private responseService : ServerResponseService ,
3133 ) {
3234 super ( ) ;
3335 }
@@ -72,8 +74,10 @@ export class ServerHardRedirectService extends HardRedirectService {
7274
7375 console . info ( `Redirecting from ${ this . req . url } to ${ redirectUrl } with ${ status } ` ) ;
7476
77+ this . setCorsHeader ( ) ;
78+
7579 this . res . redirect ( status , redirectUrl ) ;
76- this . res . end ( ) ;
80+ // this.res.end();
7781 // I haven't found a way to correctly stop Angular rendering.
7882 // So we just let it end its work, though we have already closed
7983 // the response.
@@ -96,4 +100,17 @@ export class ServerHardRedirectService extends HardRedirectService {
96100 getCurrentOrigin ( ) : string {
97101 return this . req . protocol + '://' + this . req . headers . host ;
98102 }
103+
104+ /**
105+ * Set CORS header to allow embedding of redirected content
106+ */
107+ setCorsHeader ( ) {
108+ const currentOrigin = this . getCurrentOrigin ( ) ;
109+ const allowedOrigins = this . appConfig . rest . allowedOrigins ;
110+
111+ if ( currentOrigin && allowedOrigins ?. length && allowedOrigins . includes ( currentOrigin ) ) {
112+ console . info ( 'Setting cors header for origin ' , currentOrigin ) ;
113+ this . responseService . setHeader ( 'Access-Control-Allow-Origin' , currentOrigin ) ;
114+ }
115+ }
99116}
0 commit comments