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,
withXsrfConfiguration,
HTTP_INTERCEPTORS,
withFetch,
} from '@angular/common/http';
import { ModuleWithProviders, NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@ -97,7 +98,7 @@ const CORE_COMPONENTS = [
...CORE_COMPONENTS,
],
declarations: [],
providers: [LocalizationPipe, provideHttpClient(withInterceptorsFromDi())],
providers: [LocalizationPipe, provideHttpClient(withInterceptorsFromDi(), withFetch())],
})
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 {
HTTP_INTERCEPTORS,
provideHttpClient,
withFetch,
withInterceptorsFromDi,
withXsrfConfiguration,
} from '@angular/common/http';
@ -105,6 +106,7 @@ export function provideAbpCore(...features: CoreFeature<CoreFeatureKind>[]) {
cookieName: 'XSRF-TOKEN',
headerName: 'RequestVerificationToken',
}),
withFetch(),
),
provideAppInitializer(() => {
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 { Environment } from '../models/environment';
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 { HttpErrorResponse } from '@angular/common/http';
const platformId = inject(PLATFORM_ID);
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 += '/';
const parseTokens = createTokenParser(appBaseUrl);
const token = tenancyPlaceholder.replace(/[}{]/g, '');
const tokenValue = isPlatformBrowser(platformId)
? parseTokens(inject(DOCUMENT).defaultView?.location.href)[token]?.[0]
? parseTokens(document.defaultView?.location.href)[token]?.[0]
: undefined;
return tokenValue;
}
export function getCurrentTenancyNameFromUrl(tenantKey: string): string | null {
const platformId = inject(PLATFORM_ID);
export function getCurrentTenancyNameFromUrl(tenantKey: string, injector): string | null {
const platformId = injector.get(PLATFORM_ID);
const document = injector.get(DOCUMENT);
if (!isPlatformBrowser(platformId)) {
return null;
}
const search = inject(DOCUMENT).defaultView?.location.search;
const search = document.defaultView?.location.search;
const urlParams = new URLSearchParams(search);
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 baseUrl = environmentService.getEnvironment()?.application?.baseUrl || '';
const tenancyName = getCurrentTenancyName(baseUrl);
const tenancyName = getCurrentTenancyName(baseUrl, injector);
const hideTenantBox = () => {
multiTenancyService.isTenantBoxVisible = false;
@ -91,7 +91,10 @@ export async function parseTenantFromUrl(injector: Injector) {
*/
replaceTenantNameWithinEnvironment(injector, '', tenancyPlaceholder + '.');
const tenantIdFromQueryParams = getCurrentTenancyNameFromUrl(multiTenancyService.tenantKey);
const tenantIdFromQueryParams = getCurrentTenancyNameFromUrl(
multiTenancyService.tenantKey,
injector,
);
if (tenantIdFromQueryParams) {
const tenantById$ = multiTenancyService.setTenantById(tenantIdFromQueryParams);
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> {
if (this.oAuthConfig.clientId) {
const shouldClear = shouldStorageClear(this.oAuthConfig.clientId);
if (shouldClear) clearOAuthStorage();
const shouldClear = shouldStorageClear(this.oAuthConfig.clientId, this.injector);
if (shouldClear) clearOAuthStorage(this.injector);
}
this.oAuthService.configure(this.oAuthConfig);
this.oAuthService.events
@ -122,7 +122,7 @@ export abstract class AuthFlowStrategy {
}
protected refreshToken() {
return this.oAuthService.refreshToken().catch(() => clearOAuthStorage());
return this.oAuthService.refreshToken().catch(() => clearOAuthStorage(this.injector));
}
protected listenToOauthErrors() {
@ -132,7 +132,7 @@ export abstract class AuthFlowStrategy {
tap((err: OAuthErrorEvent) => {
const shouldSkip = this.oAuthErrorFilterService.run(err);
if (!shouldSkip) {
clearOAuthStorage();
clearOAuthStorage(this.injector);
}
}),
switchMap(() => this.configState.refreshAppState()),
@ -141,8 +141,8 @@ export abstract class AuthFlowStrategy {
}
}
function shouldStorageClear(clientId: string): boolean {
const storage = inject(OAuthStorage);
function shouldStorageClear(clientId: string, injector: Injector): boolean {
const storage = injector.get(OAuthStorage);
const key = 'abpOAuthClientId';
if (!storage.getItem(key)) {
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
.pipe(
filter(
event => event instanceof OAuthInfoEvent &&
event =>
event instanceof OAuthInfoEvent &&
event.type === 'token_expires' &&
event.info === 'access_token'
event.info === 'access_token',
),
)
.subscribe(() => {
@ -82,7 +83,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
protected refreshToken() {
return this.oAuthService.refreshToken().catch(() => {
clearOAuthStorage();
clearOAuthStorage(this.injector);
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 oAuth = injector.get(OAuthService);
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 { inject } from '@angular/core';
import { Injector } from '@angular/core';
export function clearOAuthStorage() {
const storage = inject(OAuthStorage);
export function clearOAuthStorage(injector: Injector) {
const storage = injector.get(OAuthStorage);
const keys = [
'access_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 { map } from 'rxjs/operators';
import { LocaleDirection } from '../models/common';
import { DOCUMENT } from '@angular/common';
@Injectable()
export class DocumentDirHandlerService {
@ -22,7 +23,7 @@ export class DocumentDirHandlerService {
}
private setBodyDir(dir: LocaleDirection) {
document.body.dir = dir;
document.dir = dir;
this.injector.get(DOCUMENT).body.dir = dir;
this.injector.get(DOCUMENT).dir = dir;
}
}

Loading…
Cancel
Save