hermes-web-angular/src/app/auth/tts-login/tts-login.component.ts

69 lines
2.3 KiB
TypeScript
Raw Normal View History

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';
import { MatCard, MatCardModule } from '@angular/material/card';
@Component({
selector: 'tts-login',
standalone: true,
imports: [MatButtonModule, MatCardModule, 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', async _ => {
await this.router.navigate(['policies'])
});
this.events.listen('tts_logoff', async _ => {
this.selected_api_key = undefined;
await this.router.navigate(['tts-login'])
});
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() {
if (!this.selected_api_key)
return;
this.hermes.login(this.selected_api_key);
}
}