import { Component, OnDestroy, OnInit } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { MatInputModule } from '@angular/material/input'; import { MatSelectModule } from '@angular/material/select'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatButtonModule } from '@angular/material/button'; import EventService from '../shared/services/EventService'; import { HttpClient } from '@angular/common/http'; import { Router } from '@angular/router'; import { Subscription } from 'rxjs'; import { environment } from '../../environments/environment'; import { HermesClientService } from '../hermes-client.service'; @Component({ selector: 'tts-login', standalone: true, imports: [MatButtonModule, MatFormFieldModule, MatSelectModule, MatInputModule, FormsModule], templateUrl: './tts-login.component.html', styleUrl: './tts-login.component.scss' }) export class TtsLoginComponent implements OnInit, OnDestroy { api_keys: { id: string, label: string }[]; selected_api_key: string|undefined; private subscription: Subscription|undefined; constructor(private hermes: HermesClientService, private events: EventService, private http: HttpClient, private router: Router) { this.api_keys = []; } ngOnInit(): void { this.http.get(environment.API_HOST + '/keys', { headers: { 'Authorization': 'Bearer ' + localStorage.getItem('jwt') } }).subscribe((data: any) => this.api_keys = data); this.subscription = this.events.listen('tts_login_ack', _ => { if (document.location.href.includes('/tts-login')) { this.router.navigate(['/policies']) } }); this.events.listen('tts_logoff', _ => { this.selected_api_key = undefined; }); this.events.listen('impersonation', _ => { this.selected_api_key = undefined; this.http.get(environment.API_HOST + '/keys', { headers: { 'Authorization': 'Bearer ' + localStorage.getItem('jwt') } }).subscribe((data: any) => this.api_keys = data); }); } ngOnDestroy(): void { if (this.subscription) this.subscription.unsubscribe(); } login() { console.log('api key for login', this.selected_api_key) if (!this.selected_api_key) return; this.hermes.login(this.selected_api_key); } }