From b96b1709007e8cf14b968b46ba8f0b9fa0fe20f1 Mon Sep 17 00:00:00 2001 From: erdemcaygor Date: Thu, 30 Oct 2025 21:20:01 +0300 Subject: [PATCH] refactoring --- .../services/server-token-storage.service.ts | 7 +++-- .../packages/oauth/src/lib/tokens/cookies.ts | 3 ++ .../packages/oauth/src/lib/tokens/index.ts | 1 + .../files/server-builder/server.ts.template | 9 +++--- .../schematics/src/commands/ssr-add/index.ts | 28 +++++++++++++++++++ 5 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 npm/ng-packs/packages/oauth/src/lib/tokens/cookies.ts diff --git a/npm/ng-packs/packages/oauth/src/lib/services/server-token-storage.service.ts b/npm/ng-packs/packages/oauth/src/lib/services/server-token-storage.service.ts index 0926b749d8..bbbdcae543 100644 --- a/npm/ng-packs/packages/oauth/src/lib/services/server-token-storage.service.ts +++ b/npm/ng-packs/packages/oauth/src/lib/services/server-token-storage.service.ts @@ -1,13 +1,16 @@ -import { Inject, Injectable, Optional } from '@angular/core'; +import { inject, Inject, Injectable, Optional } from '@angular/core'; import { OAuthStorage } from 'angular-oauth2-oidc'; import { REQUEST } from '@angular/core'; +import { COOKIES } from '../tokens'; @Injectable({ providedIn: null }) export class ServerTokenStorageService implements OAuthStorage { private cookies = new Map(); + // For server builders where REQUEST injection is not possible, cookies can be provided via COOKIES token + private cookiesStr = inject(COOKIES, { optional: true }); constructor(@Optional() @Inject(REQUEST) private req: Request | null) { - const cookieHeader = this.req?.headers.get('cookie') ?? ''; + const cookieHeader = this.req?.headers.get('cookie') ?? this.cookiesStr ?? ''; for (const part of cookieHeader.split(';')) { const i = part.indexOf('='); if (i > -1) { diff --git a/npm/ng-packs/packages/oauth/src/lib/tokens/cookies.ts b/npm/ng-packs/packages/oauth/src/lib/tokens/cookies.ts new file mode 100644 index 0000000000..f6cf608bf6 --- /dev/null +++ b/npm/ng-packs/packages/oauth/src/lib/tokens/cookies.ts @@ -0,0 +1,3 @@ +import { InjectionToken } from '@angular/core'; + +export const COOKIES = new InjectionToken('COOKIES'); diff --git a/npm/ng-packs/packages/oauth/src/lib/tokens/index.ts b/npm/ng-packs/packages/oauth/src/lib/tokens/index.ts index 1ec58f54b3..cce552af07 100644 --- a/npm/ng-packs/packages/oauth/src/lib/tokens/index.ts +++ b/npm/ng-packs/packages/oauth/src/lib/tokens/index.ts @@ -1 +1,2 @@ export * from './auth-flow-strategy'; +export * from './cookies'; diff --git a/npm/ng-packs/packages/schematics/src/commands/ssr-add/files/server-builder/server.ts.template b/npm/ng-packs/packages/schematics/src/commands/ssr-add/files/server-builder/server.ts.template index e3102c3727..046731c7d8 100644 --- a/npm/ng-packs/packages/schematics/src/commands/ssr-add/files/server-builder/server.ts.template +++ b/npm/ng-packs/packages/schematics/src/commands/ssr-add/files/server-builder/server.ts.template @@ -2,12 +2,14 @@ import 'zone.js/node'; import { APP_BASE_HREF } from '@angular/common'; import { CommonEngine } from '@angular/ssr/node'; -import express from 'express'; +import * as express from 'express'; import { existsSync } from 'node:fs'; import { join } from 'node:path'; import <% if (isStandalone) { %>bootstrap<% } else { %>AppServerModule<% } %> from './main.server'; import {environment} from './environments/environment'; import * as oidc from 'openid-client'; +import { ServerCookieParser } from '@abp/ng.core'; +import {COOKIES} from "@abp/ng.oauth"; if (environment.production === false) { process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = "0"; @@ -176,7 +178,7 @@ export function app(): express.Express { documentFilePath: indexHtml, url: `${protocol}://${headers.host}${originalUrl}`, publicPath: distFolder, - providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }], + providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }, { provide: COOKIES, useValue: JSON.stringify(req.headers.cookie) }], }) .then((html) => { res.cookie('ssr-init', 'true', {...secureCookie, httpOnly: false}); @@ -188,7 +190,7 @@ export function app(): express.Express { return server; } -function run(): void { +async function run(): Promise { const port = process.env['PORT'] || 4000; console.log('🔐 Initializing OIDC configuration...'); @@ -201,7 +203,6 @@ function run(): void { if (error) { throw error; } - console.log(`Node Express server listening on http://localhost:${port}`); }); } diff --git a/npm/ng-packs/packages/schematics/src/commands/ssr-add/index.ts b/npm/ng-packs/packages/schematics/src/commands/ssr-add/index.ts index 17e4bb2912..4f5d466384 100644 --- a/npm/ng-packs/packages/schematics/src/commands/ssr-add/index.ts +++ b/npm/ng-packs/packages/schematics/src/commands/ssr-add/index.ts @@ -211,6 +211,33 @@ function updateRootTsConfigRule(options: SSROptions): Rule { }; } +export function updateIndexHtml(options: SSROptions): Rule { + return async (host: Tree) => { + const workspace = await getWorkspace(host); + const project = workspace.projects.get(options.project); + + if (!project) { + return; + } + + const buildOptions = project.targets.get('build')?.options; + const indexPath = buildOptions?.index as string; + + if (!indexPath || !host.exists(indexPath)) { + return; + } + + const buffer = host.read(indexPath); + if (!buffer) return; + const content = buffer.toString('utf-8'); + + const loaderDiv = `
`; + let updatedContent = content.replace(loaderDiv, ''); + host.overwrite(indexPath, updatedContent); + }; +} + + function updateApplicationBuilderWorkspaceConfigRule( projectSourceRoot: string, options: SSROptions, @@ -439,6 +466,7 @@ export default function (options: SSROptions): Rule { addServerFile(sourceRoot, options, isStandalone), addScriptsRule(options, usingApplicationBuilder), addDependencies(options, usingApplicationBuilder), + updateIndexHtml(options), ]); }; }