Browse Source

refactoring

pull/24026/head
erdemcaygor 11 months ago
parent
commit
b96b170900
  1. 7
      npm/ng-packs/packages/oauth/src/lib/services/server-token-storage.service.ts
  2. 3
      npm/ng-packs/packages/oauth/src/lib/tokens/cookies.ts
  3. 1
      npm/ng-packs/packages/oauth/src/lib/tokens/index.ts
  4. 9
      npm/ng-packs/packages/schematics/src/commands/ssr-add/files/server-builder/server.ts.template
  5. 28
      npm/ng-packs/packages/schematics/src/commands/ssr-add/index.ts

7
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<string, string>();
// For server builders where REQUEST injection is not possible, cookies can be provided via COOKIES token
private cookiesStr = inject<string>(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) {

3
npm/ng-packs/packages/oauth/src/lib/tokens/cookies.ts

@ -0,0 +1,3 @@
import { InjectionToken } from '@angular/core';
export const COOKIES = new InjectionToken<string>('COOKIES');

1
npm/ng-packs/packages/oauth/src/lib/tokens/index.ts

@ -1 +1,2 @@
export * from './auth-flow-strategy';
export * from './cookies';

9
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<void> {
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}`);
});
}

28
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 = `<div id="lp-page-loader"></div>`;
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),
]);
};
}

Loading…
Cancel
Save