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

89.18% Statements 33/37
80% Branches 8/10
85.71% Functions 12/14
88.88% Lines 32/36

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                                      151x 41x 41x 41x 41x 41x     41x       41x             41x   41x 1x       41x   41x     41x     41x           41x 685x     41x                         21x       23x 23x 23x     21x 21x 21x 21x 21x   21x 19x           1x               685x   110x      
import { Component, inject, OnInit, OnDestroy } from '@angular/core';
import { Router, RouterModule, RouterLink, ActivatedRoute } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
import { ToastModule } from 'primeng/toast';
import { MessageService } from 'primeng/api';
import { StyleService } from '../../services/style.service';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { first, interval, Subscription } from 'rxjs';
 
 
@Component({
    selector: 'app-login',
    imports: [RouterModule, RouterLink, FormsModule, ReactiveFormsModule, ToastModule],
    providers: [],
    templateUrl: './login.component.html',
    styleUrl: './login.component.scss'
})
export class LoginComponent implements OnInit, OnDestroy{
  private router = inject(Router);
  private fb = inject(FormBuilder);
  private twinpadApiService = inject(TwinpadApiService);
  private route = inject(ActivatedRoute);
  private styleService = inject(StyleService);
 
  tokenValiditySubscription: Subscription;
  messageService = inject(MessageService);
  param: string;
  email: string;
  password: string;
  company: string = "spacedreams";
  loginForm: FormGroup;
  // TODO: Associating logos to organizations ?
 
  companyLogo: string;
 
  ngOnInit() {
    this.twinpadApiService.cloudStatusBehaviorSubject$.pipe(first()).subscribe({
      next: status => {
        if (status.services.backend === "up" && this.twinpadApiService.isTokenValid()) {
          this.router.navigate(['']);
        }
      }
    });
    this.route.queryParams
      .subscribe(params => {
        this.param = params['email'];
    });
 
    this.companyLogo = this.styleService.defaultLogo;
 
    // Initialize the form
    this.loginForm = this.fb.group({
      email: [this.param, [Validators.required, Validators.email]],  // Add validation for email
      password: ['', Validators.required]
    });
 
    // Watch for email input changes
    this.loginForm.get('email')?.valueChanges.subscribe((email: string) => {
      this.updateCompanyLogo(email);
    });
 
    this.tokenValiditySubscription = interval(5000).subscribe({
      next: _ => {
        if (this.twinpadApiService.isTokenValid()) {
          this.twinpadApiService.loggedInUserBehaviorSubject$.next(true);
          setTimeout(() => {
            this.router.navigate(['']);
          }, 500);
        }
      }
    });
  }
 
  ngOnDestroy(): void {
    this.tokenValiditySubscription?.unsubscribe();
  }
 
  login(){
    const email = this.loginForm.get("email")?.value;
    localStorage.removeItem('accessToken');
    this.twinpadApiService.login(this.loginForm).subscribe(
      {
        next: value => {
          this.messageService.add({severity:'success', summary: 'Login success', detail: 'Welcome '+ email});
          localStorage.setItem('accessToken', value.access_token );
          this.tokenValiditySubscription?.unsubscribe();
          this.twinpadApiService.loggedInUserBehaviorSubject$.next(this.twinpadApiService.isTokenValid());
          this.route.queryParams.subscribe({
            next: queryParams => {
              setTimeout(() => {
                this.router.navigate(['/' + (queryParams["redirectTo"] ?? "")]);
              }, 1500);
            }
          });
        },
        error: err => {
          this.messageService.add({severity:'error', summary: 'Login failed', detail: err.error.detail});
        }
      }
    );
  }
 
  updateCompanyLogo(email: string) {
    // Parse the domain from the email
    this.companyLogo = this.styleService.getCustomerLogoFromEmail(email);
    // Set the company logo based on the domain
  }
 
}