Browse Source

refactoring

pull/24026/head
erdemcaygor 10 months ago
parent
commit
cf854ca24b
  1. 27
      npm/ng-packs/packages/schematics/src/commands/ssr-add/files/server-builder/server.ts.template
  2. 20
      npm/ng-packs/packages/schematics/src/commands/ssr-add/server/files/server-builder/ngmodule-src/app/app.module.server.ts.template

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

@ -9,15 +9,25 @@ import <% if (isStandalone) { %>bootstrap<% } else { %>AppServerModule<% } %> fr
import {environment} from './environments/environment';
import * as oidc from 'openid-client';
if (environment.production === false) {
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = "0";
}
const ISSUER = new URL(environment.oAuthConfig.issuer);
const CLIENT_ID = environment.oAuthConfig.clientId;
const REDIRECT_URI = environment.oAuthConfig.redirectUri;
const SCOPE = environment.oAuthConfig.scope;
// @ts-ignore
const CLIENT_SECRET = environment.oAuthConfig.clientSecret || undefined;
const config = await oidc.discovery(ISSUER, CLIENT_ID, /* client_secret */ undefined);
let config: Awaited<ReturnType<typeof oidc.discovery>>;
const secureCookie = { httpOnly: true, sameSite: 'lax' as const, secure: environment.production, path: '/' };
const tokenCookie = { ...secureCookie, httpOnly: false };
async function initializeOIDC() {
config = await oidc.discovery(ISSUER, CLIENT_ID, CLIENT_SECRET);
}
// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
const server = express();
@ -73,6 +83,10 @@ export function app(): express.Express {
res.clearCookie('expires_at', tokenCookie);
res.clearCookie('returnUrl', secureCookie);
if (!config) {
return res.redirect('/');
}
const endSessionEndpoint = config.serverMetadata().end_session_endpoint;
if (endSessionEndpoint) {
const logoutUrl = new URL(endSessionEndpoint);
@ -162,9 +176,12 @@ export function app(): express.Express {
documentFilePath: indexHtml,
url: `${protocol}://${headers.host}${originalUrl}`,
publicPath: distFolder,
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }, { provide: 'cookies', useValue: JSON.stringify(req.headers.cookie) }],
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
})
.then((html) => res.send(html))
.then((html) => {
res.cookie('ssr-init', 'true', {...secureCookie, httpOnly: false});
return res.send(html)
})
.catch((err) => next(err));
});
@ -174,6 +191,10 @@ export function app(): express.Express {
function run(): void {
const port = process.env['PORT'] || 4000;
console.log('🔐 Initializing OIDC configuration...');
await initializeOIDC();
console.log('✅ OIDC configuration loaded');
// Start up the Node server
const server = app();
server.listen(port, (error) => {

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

@ -1,4 +1,4 @@
import { NgModule, inject, PLATFORM_ID, TransferState, APP_INITIALIZER } from '@angular/core';
import {NgModule, inject, PLATFORM_ID, TransferState, provideAppInitializer} from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import {isPlatformServer} from "@angular/common";
@ -12,17 +12,13 @@ import { SSR_FLAG } from '@abp/ng.core';
ServerModule,
],
providers: [
{
provide: APP_INITIALIZER,
useFactory: () => {
const platformId = inject(PLATFORM_ID);
const transferState = inject<TransferState>(TransferState);
if (isPlatformServer(platformId)) {
transferState.set(SSR_FLAG, true);
}
},
multi: true
}
provideAppInitializer(() => {
const platformId = inject(PLATFORM_ID);
const transferState = inject<TransferState>(TransferState);
if (isPlatformServer(platformId)) {
transferState.set(SSR_FLAG, true);
}
}),
],
bootstrap: [<%= appComponentName %>],
})

Loading…
Cancel
Save