@@ -4,6 +4,7 @@ import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
44import { HardRedirectService } from './hard-redirect.service' ;
55import { APP_CONFIG , AppConfig } from '../../../config/app-config.interface' ;
66import { isNotEmpty } from '../../shared/empty.util' ;
7+ import { ServerResponseService } from './server-response.service' ;
78
89/**
910 * Service for performing hard redirects within the server app module
@@ -15,6 +16,7 @@ export class ServerHardRedirectService extends HardRedirectService {
1516 @Inject ( APP_CONFIG ) protected appConfig : AppConfig ,
1617 @Inject ( REQUEST ) protected req : Request ,
1718 @Inject ( RESPONSE ) protected res : Response ,
19+ private responseService : ServerResponseService ,
1820 ) {
1921 super ( ) ;
2022 }
@@ -59,8 +61,10 @@ export class ServerHardRedirectService extends HardRedirectService {
5961
6062 console . info ( `Redirecting from ${ this . req . url } to ${ redirectUrl } with ${ status } ` ) ;
6163
64+ this . setCorsHeader ( ) ;
65+
6266 this . res . redirect ( status , redirectUrl ) ;
63- this . res . end ( ) ;
67+ // this.res.end();
6468 // I haven't found a way to correctly stop Angular rendering.
6569 // So we just let it end its work, though we have already closed
6670 // the response.
@@ -83,4 +87,17 @@ export class ServerHardRedirectService extends HardRedirectService {
8387 getCurrentOrigin ( ) : string {
8488 return this . req . protocol + '://' + this . req . headers . host ;
8589 }
90+
91+ /**
92+ * Set CORS header to allow embedding of redirected content
93+ */
94+ setCorsHeader ( ) {
95+ const currentOrigin = this . getCurrentOrigin ( ) ;
96+ const allowedOrigins = this . appConfig . rest . allowedOrigins ;
97+
98+ if ( currentOrigin && allowedOrigins ?. length && allowedOrigins . includes ( currentOrigin ) ) {
99+ console . info ( 'Setting cors header for origin ' , currentOrigin ) ;
100+ this . responseService . setHeader ( 'Access-Control-Allow-Origin' , currentOrigin ) ;
101+ }
102+ }
86103}
0 commit comments