common-ui/src/lib/tenants/tenant-select/tenant-select.component.ts
2024-12-05 12:49:51 +02:00

82 lines
3.2 KiB
TypeScript

import { ChangeDetectionStrategy, Component, inject, Input } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { Title } from '@angular/platform-browser';
import { KeycloakService } from 'keycloak-angular';
import { NGXLogger } from 'ngx-logger';
import { LoadingService } from '../../loading';
import { getConfig } from '../../services';
import { selectTenantTranslations } from '../../translations/select-tenant-translations';
import { UI_ROOT } from '../../utils';
import { getKeycloakOptions } from '../keycloak-options';
import { IStoredTenantId, TenantsService } from '../services';
import { KeycloakStatusService } from '../services/keycloak-status.service';
@Component({
templateUrl: './tenant-select.component.html',
styleUrls: ['./tenant-select.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: false,
})
export class TenantSelectComponent {
readonly #uiRoot = inject(UI_ROOT);
protected readonly logger = inject(NGXLogger);
protected readonly tenantsService = inject(TenantsService);
protected storedTenants: IStoredTenantId[] = [];
protected readonly titleService = inject(Title);
protected readonly config = getConfig();
protected readonly loadingService = inject(LoadingService);
protected readonly keycloakService = inject(KeycloakService);
protected readonly keycloakStatusService = inject(KeycloakStatusService);
protected readonly form = inject(FormBuilder).group({
// eslint-disable-next-line @typescript-eslint/unbound-method
tenantId: ['', Validators.required],
});
protected readonly translations = selectTenantTranslations;
@Input() isLoggedOut = false;
@Input() noRoleLogOut = false;
constructor() {
this.#loadStoredTenants();
}
updateTenantSelection() {
const tenantId = this.form.controls.tenantId.value;
if (!tenantId) {
throw new Error('No tenant selected!');
}
this.loadingService.start();
return this.select(tenantId);
}
async select(tenantId: string) {
try {
this.logger.info('[KEYCLOAK] Initializing keycloak for tenant', tenantId);
await this.keycloakService.init(getKeycloakOptions(this.#uiRoot, this.config, tenantId));
} catch (e) {
this.logger.info('[KEYCLOAK] Init failed. Logout');
return this.keycloakService.logout();
}
const url = this.keycloakService.getKeycloakInstance().createLoginUrl({
redirectUri: this.keycloakStatusService.createLoginUrl(tenantId),
idpHint: this.config.OAUTH_IDP_HINT,
});
this.logger.info('[KEYCLOAK] Init succeeded. Logout and redirect to', url);
return this.keycloakService.logout(url);
}
removeStored(tenantId: string) {
this.tenantsService.removeStored(tenantId);
this.#loadStoredTenants();
}
protected tenantIcon(tenant: IStoredTenantId) {
return `red:${tenant.documine ? 'documine' : 'redaction'}-logo`;
}
#loadStoredTenants() {
this.storedTenants = this.tenantsService.getStoredTenants().sort((a, b) => a.tenantId.localeCompare(b.tenantId));
}
}