Skip to content

Commit cf854ca

Browse files
committed
refactoring
1 parent d3be8eb commit cf854ca

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

npm/ng-packs/packages/schematics/src/commands/ssr-add/files/server-builder/server.ts.template

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,25 @@ import <% if (isStandalone) { %>bootstrap<% } else { %>AppServerModule<% } %> fr
99
import {environment} from './environments/environment';
1010
import * as oidc from 'openid-client';
1111

12+
if (environment.production === false) {
13+
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = "0";
14+
}
15+
1216
const ISSUER = new URL(environment.oAuthConfig.issuer);
1317
const CLIENT_ID = environment.oAuthConfig.clientId;
1418
const REDIRECT_URI = environment.oAuthConfig.redirectUri;
1519
const SCOPE = environment.oAuthConfig.scope;
20+
// @ts-ignore
21+
const CLIENT_SECRET = environment.oAuthConfig.clientSecret || undefined;
1622

17-
const config = await oidc.discovery(ISSUER, CLIENT_ID, /* client_secret */ undefined);
23+
let config: Awaited<ReturnType<typeof oidc.discovery>>;
1824
const secureCookie = { httpOnly: true, sameSite: 'lax' as const, secure: environment.production, path: '/' };
1925
const tokenCookie = { ...secureCookie, httpOnly: false };
2026

27+
async function initializeOIDC() {
28+
config = await oidc.discovery(ISSUER, CLIENT_ID, CLIENT_SECRET);
29+
}
30+
2131
// The Express app is exported so that it can be used by serverless Functions.
2232
export function app(): express.Express {
2333
const server = express();
@@ -73,6 +83,10 @@ export function app(): express.Express {
7383
res.clearCookie('expires_at', tokenCookie);
7484
res.clearCookie('returnUrl', secureCookie);
7585

86+
if (!config) {
87+
return res.redirect('/');
88+
}
89+
7690
const endSessionEndpoint = config.serverMetadata().end_session_endpoint;
7791
if (endSessionEndpoint) {
7892
const logoutUrl = new URL(endSessionEndpoint);
@@ -162,9 +176,12 @@ export function app(): express.Express {
162176
documentFilePath: indexHtml,
163177
url: `${protocol}://${headers.host}${originalUrl}`,
164178
publicPath: distFolder,
165-
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }, { provide: 'cookies', useValue: JSON.stringify(req.headers.cookie) }],
179+
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
166180
})
167-
.then((html) => res.send(html))
181+
.then((html) => {
182+
res.cookie('ssr-init', 'true', {...secureCookie, httpOnly: false});
183+
return res.send(html)
184+
})
168185
.catch((err) => next(err));
169186
});
170187

@@ -174,6 +191,10 @@ export function app(): express.Express {
174191
function run(): void {
175192
const port = process.env['PORT'] || 4000;
176193

194+
console.log('🔐 Initializing OIDC configuration...');
195+
await initializeOIDC();
196+
console.log('✅ OIDC configuration loaded');
197+
177198
// Start up the Node server
178199
const server = app();
179200
server.listen(port, (error) => {

npm/ng-packs/packages/schematics/src/commands/ssr-add/server/files/server-builder/ngmodule-src/app/app.module.server.ts.template

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NgModule, inject, PLATFORM_ID, TransferState, APP_INITIALIZER } from '@angular/core';
1+
import {NgModule, inject, PLATFORM_ID, TransferState, provideAppInitializer} from '@angular/core';
22
import { ServerModule } from '@angular/platform-server';
33
import {isPlatformServer} from "@angular/common";
44

@@ -12,17 +12,13 @@ import { SSR_FLAG } from '@abp/ng.core';
1212
ServerModule,
1313
],
1414
providers: [
15-
{
16-
provide: APP_INITIALIZER,
17-
useFactory: () => {
18-
const platformId = inject(PLATFORM_ID);
19-
const transferState = inject<TransferState>(TransferState);
20-
if (isPlatformServer(platformId)) {
21-
transferState.set(SSR_FLAG, true);
22-
}
23-
},
24-
multi: true
25-
}
15+
provideAppInitializer(() => {
16+
const platformId = inject(PLATFORM_ID);
17+
const transferState = inject<TransferState>(TransferState);
18+
if (isPlatformServer(platformId)) {
19+
transferState.set(SSR_FLAG, true);
20+
}
21+
}),
2622
],
2723
bootstrap: [<%= appComponentName %>],
2824
})

0 commit comments

Comments
 (0)