Browse Source

refactoring

feature/ssr-migration-schematic
erdemcaygor 1 year ago
parent
commit
446a1087ff
  1. 3
      npm/ng-packs/packages/core/src/lib/core.module.ts
  2. 2
      npm/ng-packs/packages/core/src/lib/providers/core-module-config.provider.ts
  3. 25
      npm/ng-packs/packages/core/src/lib/utils/multi-tenancy-utils.ts
  4. 12
      npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts
  5. 7
      npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts
  6. 2
      npm/ng-packs/packages/oauth/src/lib/utils/check-access-token.ts
  7. 6
      npm/ng-packs/packages/oauth/src/lib/utils/clear-o-auth-storage.ts
  8. 5
      npm/ng-packs/packages/theme-shared/src/lib/handlers/document-dir.handler.ts

3
npm/ng-packs/packages/core/src/lib/core.module.ts

@ -4,6 +4,7 @@ import {
withInterceptorsFromDi, withInterceptorsFromDi,
withXsrfConfiguration, withXsrfConfiguration,
HTTP_INTERCEPTORS, HTTP_INTERCEPTORS,
withFetch,
} from '@angular/common/http'; } from '@angular/common/http';
import { ModuleWithProviders, NgModule } from '@angular/core'; import { ModuleWithProviders, NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@ -97,7 +98,7 @@ const CORE_COMPONENTS = [
...CORE_COMPONENTS, ...CORE_COMPONENTS,
], ],
declarations: [], declarations: [],
providers: [LocalizationPipe, provideHttpClient(withInterceptorsFromDi())], providers: [LocalizationPipe, provideHttpClient(withInterceptorsFromDi(), withFetch())],
}) })
export class BaseCoreModule {} export class BaseCoreModule {}

2
npm/ng-packs/packages/core/src/lib/providers/core-module-config.provider.ts

@ -3,6 +3,7 @@ import { TitleStrategy } from '@angular/router';
import { import {
HTTP_INTERCEPTORS, HTTP_INTERCEPTORS,
provideHttpClient, provideHttpClient,
withFetch,
withInterceptorsFromDi, withInterceptorsFromDi,
withXsrfConfiguration, withXsrfConfiguration,
} from '@angular/common/http'; } from '@angular/common/http';
@ -105,6 +106,7 @@ export function provideAbpCore(...features: CoreFeature<CoreFeatureKind>[]) {
cookieName: 'XSRF-TOKEN', cookieName: 'XSRF-TOKEN',
headerName: 'RequestVerificationToken', headerName: 'RequestVerificationToken',
}), }),
withFetch(),
), ),
provideAppInitializer(() => { provideAppInitializer(() => {
getInitialData(); getInitialData();

25
npm/ng-packs/packages/core/src/lib/utils/multi-tenancy-utils.ts

@ -1,4 +1,4 @@
import { Injector, inject, PLATFORM_ID } from '@angular/core'; import { Injector, PLATFORM_ID } from '@angular/core';
import clone from 'just-clone'; import clone from 'just-clone';
import { Environment } from '../models/environment'; import { Environment } from '../models/environment';
import { DOCUMENT, isPlatformBrowser } from '@angular/common'; import { DOCUMENT, isPlatformBrowser } from '@angular/common';
@ -11,28 +11,28 @@ import { firstValueFrom } from 'rxjs';
import { TENANT_NOT_FOUND_BY_NAME } from '../tokens/tenant-not-found-by-name'; import { TENANT_NOT_FOUND_BY_NAME } from '../tokens/tenant-not-found-by-name';
import { HttpErrorResponse } from '@angular/common/http'; import { HttpErrorResponse } from '@angular/common/http';
const platformId = inject(PLATFORM_ID);
const tenancyPlaceholder = '{0}'; const tenancyPlaceholder = '{0}';
function getCurrentTenancyName(appBaseUrl: string): string { function getCurrentTenancyName(appBaseUrl: string, injector: Injector): string {
const platformId = injector.get(PLATFORM_ID);
const document = injector.get(DOCUMENT);
if (appBaseUrl.charAt(appBaseUrl.length - 1) !== '/') appBaseUrl += '/'; if (appBaseUrl.charAt(appBaseUrl.length - 1) !== '/') appBaseUrl += '/';
const parseTokens = createTokenParser(appBaseUrl); const parseTokens = createTokenParser(appBaseUrl);
const token = tenancyPlaceholder.replace(/[}{]/g, ''); const token = tenancyPlaceholder.replace(/[}{]/g, '');
const tokenValue = isPlatformBrowser(platformId) const tokenValue = isPlatformBrowser(platformId)
? parseTokens(inject(DOCUMENT).defaultView?.location.href)[token]?.[0] ? parseTokens(document.defaultView?.location.href)[token]?.[0]
: undefined; : undefined;
return tokenValue; return tokenValue;
} }
export function getCurrentTenancyNameFromUrl(tenantKey: string): string | null { export function getCurrentTenancyNameFromUrl(tenantKey: string, injector): string | null {
const platformId = inject(PLATFORM_ID); const platformId = injector.get(PLATFORM_ID);
const document = injector.get(DOCUMENT);
if (!isPlatformBrowser(platformId)) { if (!isPlatformBrowser(platformId)) {
return null; return null;
} }
const search = document.defaultView?.location.search;
const search = inject(DOCUMENT).defaultView?.location.search;
const urlParams = new URLSearchParams(search); const urlParams = new URLSearchParams(search);
return urlParams.get(tenantKey); return urlParams.get(tenantKey);
} }
@ -43,7 +43,7 @@ export async function parseTenantFromUrl(injector: Injector) {
const tenantNotFoundHandler = injector.get(TENANT_NOT_FOUND_BY_NAME, null); const tenantNotFoundHandler = injector.get(TENANT_NOT_FOUND_BY_NAME, null);
const baseUrl = environmentService.getEnvironment()?.application?.baseUrl || ''; const baseUrl = environmentService.getEnvironment()?.application?.baseUrl || '';
const tenancyName = getCurrentTenancyName(baseUrl); const tenancyName = getCurrentTenancyName(baseUrl, injector);
const hideTenantBox = () => { const hideTenantBox = () => {
multiTenancyService.isTenantBoxVisible = false; multiTenancyService.isTenantBoxVisible = false;
@ -91,7 +91,10 @@ export async function parseTenantFromUrl(injector: Injector) {
*/ */
replaceTenantNameWithinEnvironment(injector, '', tenancyPlaceholder + '.'); replaceTenantNameWithinEnvironment(injector, '', tenancyPlaceholder + '.');
const tenantIdFromQueryParams = getCurrentTenancyNameFromUrl(multiTenancyService.tenantKey); const tenantIdFromQueryParams = getCurrentTenancyNameFromUrl(
multiTenancyService.tenantKey,
injector,
);
if (tenantIdFromQueryParams) { if (tenantIdFromQueryParams) {
const tenantById$ = multiTenancyService.setTenantById(tenantIdFromQueryParams); const tenantById$ = multiTenancyService.setTenantById(tenantIdFromQueryParams);
return firstValueFrom(tenantById$); return firstValueFrom(tenantById$);

12
npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts

@ -73,8 +73,8 @@ export abstract class AuthFlowStrategy {
async init(): Promise<any> { async init(): Promise<any> {
if (this.oAuthConfig.clientId) { if (this.oAuthConfig.clientId) {
const shouldClear = shouldStorageClear(this.oAuthConfig.clientId); const shouldClear = shouldStorageClear(this.oAuthConfig.clientId, this.injector);
if (shouldClear) clearOAuthStorage(); if (shouldClear) clearOAuthStorage(this.injector);
} }
this.oAuthService.configure(this.oAuthConfig); this.oAuthService.configure(this.oAuthConfig);
this.oAuthService.events this.oAuthService.events
@ -122,7 +122,7 @@ export abstract class AuthFlowStrategy {
} }
protected refreshToken() { protected refreshToken() {
return this.oAuthService.refreshToken().catch(() => clearOAuthStorage()); return this.oAuthService.refreshToken().catch(() => clearOAuthStorage(this.injector));
} }
protected listenToOauthErrors() { protected listenToOauthErrors() {
@ -132,7 +132,7 @@ export abstract class AuthFlowStrategy {
tap((err: OAuthErrorEvent) => { tap((err: OAuthErrorEvent) => {
const shouldSkip = this.oAuthErrorFilterService.run(err); const shouldSkip = this.oAuthErrorFilterService.run(err);
if (!shouldSkip) { if (!shouldSkip) {
clearOAuthStorage(); clearOAuthStorage(this.injector);
} }
}), }),
switchMap(() => this.configState.refreshAppState()), switchMap(() => this.configState.refreshAppState()),
@ -141,8 +141,8 @@ export abstract class AuthFlowStrategy {
} }
} }
function shouldStorageClear(clientId: string): boolean { function shouldStorageClear(clientId: string, injector: Injector): boolean {
const storage = inject(OAuthStorage); const storage = injector.get(OAuthStorage);
const key = 'abpOAuthClientId'; const key = 'abpOAuthClientId';
if (!storage.getItem(key)) { if (!storage.getItem(key)) {
storage.setItem(key, clientId); storage.setItem(key, clientId);

7
npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts

@ -15,9 +15,10 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
this.oAuthService.events this.oAuthService.events
.pipe( .pipe(
filter( filter(
event => event instanceof OAuthInfoEvent && event =>
event instanceof OAuthInfoEvent &&
event.type === 'token_expires' && event.type === 'token_expires' &&
event.info === 'access_token' event.info === 'access_token',
), ),
) )
.subscribe(() => { .subscribe(() => {
@ -82,7 +83,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
protected refreshToken() { protected refreshToken() {
return this.oAuthService.refreshToken().catch(() => { return this.oAuthService.refreshToken().catch(() => {
clearOAuthStorage(); clearOAuthStorage(this.injector);
this.rememberMeService.remove(); this.rememberMeService.remove();
}); });
} }

2
npm/ng-packs/packages/oauth/src/lib/utils/check-access-token.ts

@ -7,6 +7,6 @@ export const checkAccessToken: CheckAuthenticationStateFn = function (injector:
const configState = injector.get(ConfigStateService); const configState = injector.get(ConfigStateService);
const oAuth = injector.get(OAuthService); const oAuth = injector.get(OAuthService);
if (oAuth.hasValidAccessToken() && !configState.getDeep('currentUser.id')) { if (oAuth.hasValidAccessToken() && !configState.getDeep('currentUser.id')) {
clearOAuthStorage(); clearOAuthStorage(this.injector);
} }
}; };

6
npm/ng-packs/packages/oauth/src/lib/utils/clear-o-auth-storage.ts

@ -1,8 +1,8 @@
import { OAuthStorage } from 'angular-oauth2-oidc'; import { OAuthStorage } from 'angular-oauth2-oidc';
import { inject } from '@angular/core'; import { Injector } from '@angular/core';
export function clearOAuthStorage() { export function clearOAuthStorage(injector: Injector) {
const storage = inject(OAuthStorage); const storage = injector.get(OAuthStorage);
const keys = [ const keys = [
'access_token', 'access_token',
'id_token', 'id_token',

5
npm/ng-packs/packages/theme-shared/src/lib/handlers/document-dir.handler.ts

@ -3,6 +3,7 @@ import { Injectable, Injector } from '@angular/core';
import { BehaviorSubject } from 'rxjs'; import { BehaviorSubject } from 'rxjs';
import { map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { LocaleDirection } from '../models/common'; import { LocaleDirection } from '../models/common';
import { DOCUMENT } from '@angular/common';
@Injectable() @Injectable()
export class DocumentDirHandlerService { export class DocumentDirHandlerService {
@ -22,7 +23,7 @@ export class DocumentDirHandlerService {
} }
private setBodyDir(dir: LocaleDirection) { private setBodyDir(dir: LocaleDirection) {
document.body.dir = dir; this.injector.get(DOCUMENT).body.dir = dir;
document.dir = dir; this.injector.get(DOCUMENT).dir = dir;
} }
} }

Loading…
Cancel
Save