tenants debugging
This commit is contained in:
parent
efabd98719
commit
b4364900b5
@ -1,11 +1,10 @@
|
||||
import { inject, Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { BehaviorSubject, Observable, of } from 'rxjs';
|
||||
import { BehaviorSubject } from 'rxjs';
|
||||
import { NGXLogger } from 'ngx-logger';
|
||||
import { tap } from 'rxjs/operators';
|
||||
import { IqserConfigService } from '../../services/iqser-config.service';
|
||||
import { IqserConfigService } from '../../services';
|
||||
import { TenantContextHolder } from './tenant-context-holder';
|
||||
import { ActivatedRoute } from '@angular/router';
|
||||
import { BASE_HREF } from '../../utils';
|
||||
|
||||
export interface IBaseTenant {
|
||||
@ -22,10 +21,12 @@ export class TenantContext {
|
||||
protected readonly _http = inject(HttpClient);
|
||||
protected readonly _configService = inject(IqserConfigService);
|
||||
protected readonly _tenantContextHolder = inject(TenantContextHolder);
|
||||
protected readonly _route = inject(ActivatedRoute);
|
||||
protected _storageReference: any;
|
||||
protected _storageReference: Storage;
|
||||
|
||||
constructor(private readonly _logger: NGXLogger) {
|
||||
this._storageReference = {
|
||||
length: localStorage.length,
|
||||
clear: localStorage.clear.bind(localStorage),
|
||||
getItem: localStorage.getItem.bind(localStorage),
|
||||
setItem: localStorage.setItem.bind(localStorage),
|
||||
removeItem: localStorage.removeItem.bind(localStorage),
|
||||
@ -34,20 +35,30 @@ export class TenantContext {
|
||||
}
|
||||
|
||||
loadTenants() {
|
||||
const base = inject(BASE_HREF);
|
||||
const path = window.location.pathname;
|
||||
const nextSlash = path.indexOf('/', base.length + 1);
|
||||
const tenant = path.substring(base.length + 1, nextSlash >= 0 ? nextSlash : path.length);
|
||||
const tenant = this.getTenantFromRoute();
|
||||
|
||||
return this._http.get<IBaseTenant[]>('/tenants/simple').pipe(
|
||||
tap(tenants => {
|
||||
console.log('tenants: ', tenants);
|
||||
this.hasMultipleTenants = tenants.length > 1;
|
||||
this.tenantData$.next(tenants);
|
||||
this.tenantSelected(tenant);
|
||||
if (this.hasMultipleTenants) {
|
||||
this.tenantSelected(tenant);
|
||||
} else {
|
||||
this.tenantSelected(tenant.length ? tenant : tenants[0].tenantId);
|
||||
}
|
||||
this.tenantsReady$.next(true);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
getTenantFromRoute() {
|
||||
const base = inject(BASE_HREF);
|
||||
const path = window.location.pathname;
|
||||
const nextSlash = path.indexOf('/', base.length + 1);
|
||||
return path.substring(base.length + 1, nextSlash >= 0 ? nextSlash : path.length);
|
||||
}
|
||||
|
||||
tenantSelected(tenantId: string) {
|
||||
if (this.tenantData$.value?.map(t => t.tenantId).includes(tenantId)) {
|
||||
this._mutateStorage(tenantId);
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
import { Component, inject } from '@angular/core';
|
||||
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { FormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
|
||||
import { TenantContext } from '../services/tenant-context';
|
||||
import { IconButtonTypes } from '../../buttons/types/icon-button.type';
|
||||
import { FormBuilder, Validators } from '@angular/forms';
|
||||
import { TenantContext } from '../services';
|
||||
import { IconButtonTypes } from '../../buttons';
|
||||
import { KeycloakService } from 'keycloak-angular';
|
||||
import { BASE_HREF } from '../../utils';
|
||||
import { LoadingService } from '../../loading';
|
||||
@ -11,22 +11,23 @@ import { LoadingService } from '../../loading';
|
||||
selector: 'iqser-tenant-resolve',
|
||||
templateUrl: './tenant-resolve.component.html',
|
||||
styleUrls: ['./tenant-resolve.component.scss'],
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
})
|
||||
export class TenantResolveComponent {
|
||||
private readonly _route = inject(ActivatedRoute);
|
||||
|
||||
readonly loadingService = inject(LoadingService);
|
||||
readonly iconButtonTypes = IconButtonTypes;
|
||||
form;
|
||||
protected _tenantContext = inject(TenantContext);
|
||||
private readonly _route = inject(ActivatedRoute);
|
||||
private readonly _router = inject(Router);
|
||||
private readonly _formBuilder = inject(FormBuilder);
|
||||
private readonly _baseHref = inject(BASE_HREF);
|
||||
private readonly _keycloakService = inject(KeycloakService);
|
||||
protected _tenantContext = inject(TenantContext);
|
||||
readonly iconButtonTypes = IconButtonTypes;
|
||||
form?: UntypedFormGroup;
|
||||
|
||||
constructor() {
|
||||
this.loadingService.start();
|
||||
this.form = this._formBuilder.group({
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
tenantId: [undefined, Validators.required],
|
||||
});
|
||||
if (this._route.snapshot.paramMap.get('tenant')) {
|
||||
@ -39,6 +40,7 @@ export class TenantResolveComponent {
|
||||
} else {
|
||||
if (!this._tenantContext.hasMultipleTenants) {
|
||||
const singleTenant = this._tenantContext.tenantData$.value[0];
|
||||
console.log('single tenant: ', singleTenant);
|
||||
window.location.href = window.location.origin + this._baseHref + '/' + singleTenant.tenantId;
|
||||
}
|
||||
}
|
||||
@ -46,8 +48,7 @@ export class TenantResolveComponent {
|
||||
}
|
||||
|
||||
async updateTenantSelection() {
|
||||
console.log('update selection');
|
||||
const tenant = this.form.get('tenantId').value;
|
||||
const tenant = this.form.controls.tenantId.value;
|
||||
console.log('update to: ', tenant);
|
||||
window.location.href = window.location.origin + this._baseHref + '/' + tenant;
|
||||
}
|
||||
|
||||
@ -36,6 +36,7 @@ function getKeycloakOptions(baseUrl: string, tenantContextHolder: TenantContextH
|
||||
onLoad: 'check-sso',
|
||||
silentCheckSsoRedirectUri: window.location.origin + baseUrl + '/assets/oauth/silent-refresh.html',
|
||||
flow: 'standard',
|
||||
enableLogging: true,
|
||||
},
|
||||
enableBearerInterceptor: true,
|
||||
};
|
||||
@ -43,8 +44,15 @@ function getKeycloakOptions(baseUrl: string, tenantContextHolder: TenantContextH
|
||||
|
||||
function configureAutomaticRedirectToLoginScreen(keyCloakService: KeycloakService, keycloakStatusService: KeycloakStatusService) {
|
||||
keyCloakService.getKeycloakInstance().onAuthRefreshError = () => {
|
||||
console.log('onAuthRefreshError');
|
||||
keycloakStatusService.createLoginUrlAndExecute();
|
||||
};
|
||||
keyCloakService.getKeycloakInstance().onAuthError = err => {
|
||||
console.log('onAuthError', err);
|
||||
};
|
||||
keyCloakService.getKeycloakInstance().onActionUpdate = err => {
|
||||
console.log('onaction update', err);
|
||||
};
|
||||
}
|
||||
|
||||
export function keycloakInitializer(
|
||||
@ -58,12 +66,14 @@ export function keycloakInitializer(
|
||||
const tenantsReady = tenantContext.tenantsReady$.pipe(
|
||||
filter(t => t),
|
||||
switchMap(() => {
|
||||
console.log('keycloak init for: ', tenantContextHolder.currentTenant);
|
||||
if (tenantContextHolder.currentTenant) {
|
||||
const x = keycloakService.init(getKeycloakOptions(baseUrl, tenantContextHolder, configService));
|
||||
configureAutomaticRedirectToLoginScreen(keycloakService, keycloakStatusService);
|
||||
keycloakStatusService.updateStatus(KeycloakStatus.PENDING);
|
||||
configureAutomaticRedirectToLoginScreen(keycloakService, keycloakStatusService);
|
||||
return x;
|
||||
} else {
|
||||
console.log('keycloak init skipped');
|
||||
keycloakStatusService.updateStatus(KeycloakStatus.NOT_ACTIVE);
|
||||
return of(true);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user