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

100% Statements 29/29
100% Branches 8/8
100% Functions 12/12
100% Lines 25/25

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                                    104x 33x       33x           33x 33x     33x   33x 2x       33x   33x     33x     33x           33x 369x         13x 13x 13x     11x 11x 11x 11x   11x 10x           1x               369x          
import { Component, inject, OnInit } 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';
 
 
@Component({
    selector: 'app-login',
    imports: [RouterModule, RouterLink, FormsModule, ReactiveFormsModule, ToastModule],
    providers: [],
    templateUrl: './login.component.html',
    styleUrl: './login.component.scss'
})
export class LoginComponent implements OnInit{
  messageService = inject(MessageService);
  param: string;
  email: string;
  password: string;
  company: string = "spacedreams";
  loginForm: FormGroup;
  // TODO: Associating logos to organizations ?
 
  companyLogo: string;
 
  constructor(private router: Router, private fb: FormBuilder, private twinpadApiService: TwinpadApiService,
     private route: ActivatedRoute, private styleService: StyleService){}
 
  ngOnInit() {
    this.twinpadApiService.cloudStatusBehaviorSubject$.subscribe({
      next: status => {
        if (status.services.backend === "up" && this.twinpadApiService.isTokenValid()) {
          this.router.navigate(['']);
        }
      }
    }).unsubscribe();
    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);
    });
  }
 
  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.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
  }
 
}