All files / src/app/features/login login.component.ts

81.08% Statements 30/37
50% Branches 6/12
71.42% Functions 5/7
100% Lines 27/27

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 842x 2x 2x 2x 2x 2x 2x 2x 2x     2x                                 2x   9x     9x 9x 9x 9x 9x 9x                                     8x 5x 5x 5x       3x     3x               2x 2x 2x            
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators, ReactiveFormsModule } from '@angular/forms';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatButtonModule } from '@angular/material/button';
import { MatInputModule } from '@angular/material/input';
import { Router, RouterLink } from '@angular/router';
import { SessionService } from '@core/services/session.service';
import { AuthService } from '@core/services/auth.service';
import { catchError, throwError } from 'rxjs';
import { ApiError } from '@core/errors/api-error';
import { LoginRequest } from '@core/model/login-request.interface';
import { ErrorProcessorService } from '@core/services/error-processor.service';
import { SessionInformation } from '@core/model/session-information.interface';
 
 
@Component({
  selector: 'app-login',
  standalone: true,
  imports: [
    MatButtonModule,
    MatFormFieldModule,
    MatInputModule,
    ReactiveFormsModule,
    RouterLink
  ],
  templateUrl: './login.component.html',
  styleUrl: './login.component.scss'
})
export class LoginComponent {
  public form:FormGroup;
  public isSubmitting = false;
 
  constructor(
    private authService: AuthService,
    private fb: FormBuilder,
    private sessionService: SessionService,
    private router: Router,
    private errorProcessor: ErrorProcessorService) {
      this.form = this.fb.group({
        email: [
          '', 
          [
            Validators.required,
            Validators.email
          ]
        ],
        password: [
          '',
          [
            Validators.required,
            Validators.min(3)
          ]
        ]
      });
  }
 
  public submit() :void {
    if(!this.isSubmitting && this.form.valid) {
      this.isSubmitting = true;
      const loginRequest: LoginRequest = this.form.value as LoginRequest;
      this.authService.login(loginRequest)
        .pipe(
          catchError(
            (error: ApiError) => {
              this.isSubmitting = false;
              // here we have to build a "regular" Error
              // otherwise the NotificationService would not display as it is specifically designed to skip  401 errors
              return this.errorProcessor.processError(new Error(
                $localize `:@@error.login:Login failed`+'.'+(error.httpStatus === 401 ? ' '+$localize `:@@info.login.verify:Please verify your email or password` +'.' : '')
              ));
            }
          )
        )
        .subscribe(
          (response: SessionInformation) => {
            this.isSubmitting = false;
            this.sessionService.logIn(response);
            this.router.navigate(['/jobs'])
          }
        )
    }
  }
}