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

100% Statements 26/26
100% Branches 4/4
100% Functions 10/10
100% Lines 22/22

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                                    75x 23x       23x           23x 23x     23x   1x   23x   23x     23x     23x           23x 241x         9x 9x 9x     7x 7x 7x 7x       1x               241x          
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() {
    if(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);
    });
  }
 
  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 );
          setTimeout(() => {
            this.router.navigate(['/']);
          }, 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
  }
 
}