Skip to content

Commit b96b170

Browse files
committed
refactoring
1 parent cf854ca commit b96b170

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

npm/ng-packs/packages/oauth/src/lib/services/server-token-storage.service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import { Inject, Injectable, Optional } from '@angular/core';
1+
import { inject, Inject, Injectable, Optional } from '@angular/core';
22
import { OAuthStorage } from 'angular-oauth2-oidc';
33
import { REQUEST } from '@angular/core';
4+
import { COOKIES } from '../tokens';
45

56
@Injectable({ providedIn: null })
67
export class ServerTokenStorageService implements OAuthStorage {
78
private cookies = new Map<string, string>();
9+
// For server builders where REQUEST injection is not possible, cookies can be provided via COOKIES token
10+
private cookiesStr = inject<string>(COOKIES, { optional: true });
811

912
constructor(@Optional() @Inject(REQUEST) private req: Request | null) {
10-
const cookieHeader = this.req?.headers.get('cookie') ?? '';
13+
const cookieHeader = this.req?.headers.get('cookie') ?? this.cookiesStr ?? '';
1114
for (const part of cookieHeader.split(';')) {
1215
const i = part.indexOf('=');
1316
if (i > -1) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { InjectionToken } from '@angular/core';
2+
3+
export const COOKIES = new InjectionToken<string>('COOKIES');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './auth-flow-strategy';
2+
export * from './cookies';

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import 'zone.js/node';
22

33
import { APP_BASE_HREF } from '@angular/common';
44
import { CommonEngine } from '@angular/ssr/node';
5-
import express from 'express';
5+
import * as express from 'express';
66
import { existsSync } from 'node:fs';
77
import { join } from 'node:path';
88
import <% if (isStandalone) { %>bootstrap<% } else { %>AppServerModule<% } %> from './main.server';
99
import {environment} from './environments/environment';
1010
import * as oidc from 'openid-client';
11+
import { ServerCookieParser } from '@abp/ng.core';
12+
import {COOKIES} from "@abp/ng.oauth";
1113

1214
if (environment.production === false) {
1315
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = "0";
@@ -176,7 +178,7 @@ export function app(): express.Express {
176178
documentFilePath: indexHtml,
177179
url: `${protocol}://${headers.host}${originalUrl}`,
178180
publicPath: distFolder,
179-
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
181+
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }, { provide: COOKIES, useValue: JSON.stringify(req.headers.cookie) }],
180182
})
181183
.then((html) => {
182184
res.cookie('ssr-init', 'true', {...secureCookie, httpOnly: false});
@@ -188,7 +190,7 @@ export function app(): express.Express {
188190
return server;
189191
}
190192

191-
function run(): void {
193+
async function run(): Promise<void> {
192194
const port = process.env['PORT'] || 4000;
193195

194196
console.log('🔐 Initializing OIDC configuration...');
@@ -201,7 +203,6 @@ function run(): void {
201203
if (error) {
202204
throw error;
203205
}
204-
205206
console.log(`Node Express server listening on http://localhost:${port}`);
206207
});
207208
}

npm/ng-packs/packages/schematics/src/commands/ssr-add/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,33 @@ function updateRootTsConfigRule(options: SSROptions): Rule {
211211
};
212212
}
213213

214+
export function updateIndexHtml(options: SSROptions): Rule {
215+
return async (host: Tree) => {
216+
const workspace = await getWorkspace(host);
217+
const project = workspace.projects.get(options.project);
218+
219+
if (!project) {
220+
return;
221+
}
222+
223+
const buildOptions = project.targets.get('build')?.options;
224+
const indexPath = buildOptions?.index as string;
225+
226+
if (!indexPath || !host.exists(indexPath)) {
227+
return;
228+
}
229+
230+
const buffer = host.read(indexPath);
231+
if (!buffer) return;
232+
const content = buffer.toString('utf-8');
233+
234+
const loaderDiv = `<div id="lp-page-loader"></div>`;
235+
let updatedContent = content.replace(loaderDiv, '');
236+
host.overwrite(indexPath, updatedContent);
237+
};
238+
}
239+
240+
214241
function updateApplicationBuilderWorkspaceConfigRule(
215242
projectSourceRoot: string,
216243
options: SSROptions,
@@ -439,6 +466,7 @@ export default function (options: SSROptions): Rule {
439466
addServerFile(sourceRoot, options, isStandalone),
440467
addScriptsRule(options, usingApplicationBuilder),
441468
addDependencies(options, usingApplicationBuilder),
469+
updateIndexHtml(options),
442470
]);
443471
};
444472
}

0 commit comments

Comments
 (0)