All files / landing-page/login-basic si-login-basic.component.ts

92.45% Statements 49/53
63.63% Branches 7/11
100% Functions 16/16
91.66% Lines 44/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225                                                                                                                1x 1x                 6x                 6x       6x                 6x 6x         6x                 6x           6x           6x             6x                 6x                 6x       6x       6x       6x   6x 6x 6x   6x 6x 6x   6x           6x 5x     6x   2x       1x       1x       5x       3x 1x   3x     2x 2x             3x       9x 9x       1x 1x       2x 2x 2x        
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import {
  booleanAttribute,
  Component,
  input,
  OnInit,
  output,
  signal,
  DestroyRef,
  inject,
  ChangeDetectionStrategy
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { Link, SiLinkDirective } from '@siemens/element-ng/link';
import { SiLoadingButtonComponent } from '@siemens/element-ng/loading-spinner';
import { SiPasswordToggleComponent } from '@siemens/element-ng/password-toggle';
import { SiTranslatePipe, t } from '@siemens/element-translate-ng/translate';
 
import { UsernamePassword, UsernameValidationPayload } from '../si-landing-page.model';
 
/**
 * A basic login component that supports both single-step and two-step authentication flows.
 *
 * This component provides a standard login form with username and password fields.
 * It can be configured to work in two-step mode where username validation happens first,
 * followed by password entry.
 *
 * @example
 * ```html
 * <si-login-basic
 *   [forgotPasswordLink]="forgotPasswordLink"
 *   [registerNowLink]="registerNowLink"
 *   [twoStep]="true"
 *   [loading]="isLoading"
 *   (login)="handleLogin($event)"
 *   (usernameValidation)="handleUsernameValidation($event)">
 * </si-login-basic>
 * ```
 */
@Component({
  selector: 'si-login-basic',
  imports: [
    ReactiveFormsModule,
    SiTranslatePipe,
    SiLinkDirective,
    SiPasswordToggleComponent,
    SiLoadingButtonComponent
  ],
  templateUrl: './si-login-basic.component.html',
  styleUrl: './si-login-basic.component.scss',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SiLoginBasicComponent implements OnInit {
  private static idCounter = 0;
  /**
   * Label for username input field.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_LOGIN_BASIC.USERNAME:Username`)
   * ```
   */
  readonly usernameLabel = input(t(() => $localize`:@@SI_LOGIN_BASIC.USERNAME:Username`));
  /**
   * Label for password input field.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_LOGIN_BASIC.PASSWORD:Password`)
   * ```
   */
  readonly passwordLabel = input(t(() => $localize`:@@SI_LOGIN_BASIC.PASSWORD:Password`));
  /**
   * Config for Forgot Password link.
   */
  readonly forgotPasswordLink = input<Link>();
  /**
   * Text for register now.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_LOGIN_BASIC.REGISTER_NOW_INTRO:Don't have an account?`)
   * ```
   */
  readonly registerNowIntroText = input(
    t(() => $localize`:@@SI_LOGIN_BASIC.REGISTER_NOW_INTRO:Don't have an account?`)
  );
  /**
   * Config for Register Now link.
   */
  readonly registerNowLink = input<Link>();
  /**
   * Label for login button.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_LOGIN_BASIC.LOGIN:Login`)
   * ```
   */
  readonly loginButtonLabel = input(t(() => $localize`:@@SI_LOGIN_BASIC.LOGIN:Login`));
  /**
   * Disables the login button.
   *
   * @defaultValue false
   */
  readonly disableLogin = input(false);
  /**
   * Enables the two-step login flow.
   *
   * @defaultValue false
   */
  readonly twoStep = input(false, { transform: booleanAttribute });
 
  /**
   * Indicates whether the login button should show a loading state.
   *
   * @defaultValue false
   */
  readonly loading = input(false);
  /**
   * Description of back button.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_LOGIN_BASIC.BACK:Back`)
   * ```
   */
  readonly backButtonLabel = input(t(() => $localize`:@@SI_LOGIN_BASIC.BACK:Back`));
  /**
   * Description of next button.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_LOGIN_BASIC.NEXT:Next`)
   * ```
   */
  readonly nextButtonLabel = input(t(() => $localize`:@@SI_LOGIN_BASIC.NEXT:Next`));
  /**
   * Emits username when user clicks on next button.
   */
  readonly usernameValidation = output<UsernameValidationPayload>();
  /**
   * Emits username and password on value change.
   */
  readonly valueChanged = output<UsernamePassword>();
  /**
   * Emits username and password on login event.
   */
  readonly login = output<UsernamePassword>();
 
  private destroyRef = inject(DestroyRef);
  protected readonly secondStepVisible = signal(false);
  protected readonly loadingNext = signal(false);
 
  private readonly index = SiLoginBasicComponent.idCounter++;
  protected usernameId = `__si-login-basic-username-${this.index}`;
  protected passwordId = `__si-login-basic-password-${this.index}`;
 
  protected loginCredentials = new FormGroup({
    username: new FormControl('', Validators.required),
    password: new FormControl('', Validators.required)
  });
 
  ngOnInit(): void {
    this.loginCredentials.valueChanges.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(() => {
      this.handleValueChanges();
    });
 
    this.loginCredentials.controls.username.valueChanges
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe(() => this.secondStepVisible() && this.goBack());
  }
 
  protected submit(): void {
    Iif (this.twoStep() && !this.secondStepVisible()) {
      this.validateUsername();
      return;
    }
    this.login.emit(this.getLoginFormValues());
  }
 
  protected handleValueChanges(): void {
    this.valueChanged.emit(this.getLoginFormValues());
  }
 
  protected validateUsername(): void {
    if (this.loginCredentials.controls.username.valid) {
      this.loadingNext.set(true);
    }
    const activateArgs = {
      ...this.getLoginFormValues(),
      validate: (isValid: boolean): void => {
        if (isValid) {
          this.nextStep();
        } else E{
          this.loadingNext.set(false);
          this.loginCredentials.controls.username.setErrors({ invalid: true });
        }
      }
    };
    this.usernameValidation.emit(activateArgs);
  }
 
  private getLoginFormValues(): UsernamePassword {
    const { username: username, password } = this.loginCredentials.value;
    return { username: username, password };
  }
 
  protected goBack(): void {
    this.secondStepVisible.set(false);
    this.loginCredentials.controls.password.reset();
  }
 
  private nextStep(): void {
    if (this.twoStep() && !this.secondStepVisible()) {
      this.loadingNext.set(false);
      this.secondStepVisible.set(true);
    }
  }
}