import { AsyncPipe, NgTemplateOutlet } from '@angular/common'; import { ChangeDetectionStrategy, ChangeDetectorRef, Component, computed, effect, type EffectRef, forwardRef, inject, input, Renderer2, type Signal, type TemplateRef, type Type, viewChild, ViewContainerRef } from '@angular/core'; import { Meta, Title } from '@angular/platform-browser'; import { KcSanitizePipe } from '@keycloakify/angular/lib/pipes/kc-sanitize'; import { USE_DEFAULT_CSS } from '@keycloakify/angular/lib/tokens/use-default-css'; import { ComponentReference } from '@keycloakify/angular/login/classes/component-reference'; import type { TemplateSlots } from '@keycloakify/angular/login/classes/template-slots'; import { KcClassDirective } from '@keycloakify/angular/login/directives/kc-class'; import type { I18n } from '@keycloakify/angular/login/i18n'; import { type KcContext } from '@keycloakify/angular/login/KcContext'; import { LoginResourceInjectorService } from '@keycloakify/angular/login/services/login-resource-injector'; import { LOGIN_CLASSES } from '@keycloakify/angular/login/tokens/classes'; import { LOGIN_I18N } from '@keycloakify/angular/login/tokens/i18n'; import { KC_LOGIN_CONTEXT } from '@keycloakify/angular/login/tokens/kc-context'; import { type ClassKey, getKcClsx } from 'keycloakify/login/lib/kcClsx'; import type { Observable } from 'rxjs'; @Component({ selector: 'kc-root', templateUrl: 'template.component.html', imports: [AsyncPipe, KcSanitizePipe, NgTemplateOutlet, KcClassDirective], changeDetection: ChangeDetectionStrategy.OnPush, providers: [ { provide: ComponentReference, useExisting: forwardRef(() => TemplateComponent) } ] }) export class TemplateComponent extends ComponentReference { i18n = inject(LOGIN_I18N); renderer = inject(Renderer2); #cdr = inject(ChangeDetectorRef); #effectRef: EffectRef; meta = inject(Meta); title = inject(Title); kcContext = inject(KC_LOGIN_CONTEXT); override doUseDefaultCss = inject(USE_DEFAULT_CSS); override classes = inject>>(LOGIN_CLASSES); loginResourceInjectorService = inject(LoginResourceInjectorService); displayInfo = false; displayMessage = true; displayRequiredFields = false; documentTitle: string | undefined; bodyClassName: string | undefined; isReadyToRender$: Observable; page = input>(); pageRef = viewChild('pageRef', { read: ViewContainerRef }); userProfileFormFields = input>(); headerNode: Signal | undefined> | undefined; infoNode: Signal | undefined> | undefined; socialProvidersNode: Signal | undefined> | undefined; constructor() { super(); this.isReadyToRender$ = this.loginResourceInjectorService.injectResource(this.doUseDefaultCss); this.#effectRef = effect( () => { const page = this.page(); const pageRef = this.pageRef(); if (!page || !pageRef) return; const userProfileFormFields = this.userProfileFormFields(); const compRef = pageRef.createComponent(page); if ('userProfileFormFields' in (compRef.instance as object) && userProfileFormFields) { compRef.setInput('userProfileFormFields', userProfileFormFields); } this.onComponentCreated(compRef.instance as TemplateSlots); }, { manualCleanup: true } ); } private applyKcIndexClasses() { const kcClsx = getKcClsx({ doUseDefaultCss: this.doUseDefaultCss, classes: this.classes }).kcClsx; const kcBodyClass = this.bodyClassName ?? kcClsx('kcBodyClass'); const kcHtmlClass = kcClsx('kcHtmlClass'); const kcBodyClasses = kcBodyClass.split(/\s+/); const kcHtmlClasses = kcHtmlClass.split(/\s+/); kcBodyClasses.forEach(klass => { this.renderer.addClass(document.body, klass); }); kcHtmlClasses.forEach(klass => { this.renderer.addClass(document.documentElement, klass); }); } tryAnotherWay() { document.forms['kc-select-try-another-way-form' as never].requestSubmit(); } onComponentCreated(compRef: TemplateSlots) { if ('displayInfo' in compRef) { this.displayInfo = !!compRef.displayInfo; } if ('displayMessage' in compRef) { this.displayMessage = !!compRef.displayMessage; } if ('displayRequiredFields' in compRef) { this.displayRequiredFields = !!compRef.displayRequiredFields; } if ('documentTitle' in compRef && compRef.documentTitle) { this.documentTitle = compRef.documentTitle; } if ('bodyClassName' in compRef && compRef.bodyClassName) { this.bodyClassName = compRef.bodyClassName; } if ('headerNode' in compRef && compRef.headerNode) { const headerNodeSignal = compRef.headerNode; this.headerNode = computed(() => headerNodeSignal()); } if ('infoNode' in compRef && compRef.infoNode) { const infoNodeSignal = compRef.infoNode; this.infoNode = computed(() => infoNodeSignal()); } if ('socialProvidersNode' in compRef && compRef.socialProvidersNode) { const socialProvidersNodeSignal = compRef.socialProvidersNode; this.socialProvidersNode = computed(() => socialProvidersNodeSignal()); } this.title.setTitle(this.documentTitle ?? this.i18n.msgStr('loginTitle', this.kcContext.realm.displayName)); this.applyKcIndexClasses(); this.#cdr.markForCheck(); this.#effectRef.destroy(); } }