From b465f0a4742b5ac99c25bd86275e5184151c448e Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 2 Apr 2025 19:56:21 +0000 Subject: [PATCH] Fixed API keys not swapping properly when impersonating. --- src/app/auth/tts-login/tts-login.component.ts | 26 +++++++++++++++++-- .../shared/services/api/api-key.service.ts | 8 +++--- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/app/auth/tts-login/tts-login.component.ts b/src/app/auth/tts-login/tts-login.component.ts index e3b7aa1..38c3ab3 100644 --- a/src/app/auth/tts-login/tts-login.component.ts +++ b/src/app/auth/tts-login/tts-login.component.ts @@ -1,4 +1,4 @@ -import { Component, inject, OnInit } from '@angular/core'; +import { Component, inject, OnDestroy, OnInit } from '@angular/core'; import { FormControl, ReactiveFormsModule } from '@angular/forms'; import { MatSelectModule } from '@angular/material/select'; import { MatFormFieldModule } from '@angular/material/form-field'; @@ -6,6 +6,9 @@ import { MatButtonModule } from '@angular/material/button'; import { ActivatedRoute } from '@angular/router'; import { HermesClientService } from '../../hermes-client.service'; import { MatCardModule } from '@angular/material/card'; +import EventService from '../../shared/services/EventService'; +import { Subscription } from 'rxjs'; +import { ApiKeyService } from '../../shared/services/api/api-key.service'; @Component({ selector: 'tts-login', @@ -20,16 +23,30 @@ import { MatCardModule } from '@angular/material/card'; templateUrl: './tts-login.component.html', styleUrl: './tts-login.component.scss' }) -export class TtsLoginComponent implements OnInit { +export class TtsLoginComponent implements OnInit, OnDestroy { private readonly client = inject(HermesClientService); private readonly route = inject(ActivatedRoute); + private readonly keyService = inject(ApiKeyService); + private readonly eventService = inject(EventService); keyControl = new FormControl(''); api_keys: { id: string, label: string }[] = []; + subscriptions: (Subscription | null)[] = []; ngOnInit(): void { this.route.data.subscribe(d => this.api_keys = d['keys']); + + this.subscriptions.push(this.eventService.listen('impersonation', _ => this.reset())); + this.subscriptions.push(this.eventService.listen('logoff', _ => this.reset())); + } + + ngOnDestroy(): void { + for (let subscription of this.subscriptions) { + if (subscription) { + subscription.unsubscribe(); + } + } } login(): void { @@ -38,4 +55,9 @@ export class TtsLoginComponent implements OnInit { this.client.login(this.keyControl.value); } + + private reset() { + this.api_keys = []; + this.keyService.fetch().subscribe(keys => this.api_keys = keys); + } } diff --git a/src/app/shared/services/api/api-key.service.ts b/src/app/shared/services/api/api-key.service.ts index de2d310..c19edd1 100644 --- a/src/app/shared/services/api/api-key.service.ts +++ b/src/app/shared/services/api/api-key.service.ts @@ -16,11 +16,9 @@ export class ApiKeyService { constructor() { - this.events.listen('tts_logoff', (impersonation) => { - if (impersonation) { - this.keys = []; - this.loaded = false; - } + this.events.listen('impersonation', () => { + this.keys = []; + this.loaded = false; }); this.events.listen('logoff', () => {