Move some subscriptions' lifespan. Added automatic TTS login (when there's an API key)

This commit is contained in:
Tom 2025-01-15 20:45:26 +00:00
parent 6ee99466f8
commit a19a7a0217
2 changed files with 23 additions and 12 deletions

View File

@ -1,13 +1,15 @@
import { isPlatformBrowser } from '@angular/common';
import { Component, OnInit, Inject, PLATFORM_ID, NgZone, OnDestroy } from '@angular/core';
import { Component, OnInit, Inject, PLATFORM_ID, NgZone, OnDestroy, inject } from '@angular/core';
import { Router, RouterOutlet } from '@angular/router';
import { HermesClientService } from './hermes-client.service';
import { AuthUserGuard } from './shared/auth/auth.user.guard'
import { Subscription } from 'rxjs';
import { first, Subscription, timeout } from 'rxjs';
import { NavigationComponent } from "./navigation/navigation.component";
import EventService from './shared/services/EventService';
import { ApiAuthenticationService } from './shared/services/api/api-authentication.service';
import { AuthModule } from './auth/auth.module';
import { ApiKeyService } from './shared/services/api/api-key.service';
import ApiKey from './shared/models/api-key';
@Component({
selector: 'app-root',
@ -18,6 +20,8 @@ import { AuthModule } from './auth/auth.module';
styleUrl: './app.component.scss'
})
export class AppComponent implements OnInit, OnDestroy {
private readonly keyService = inject(ApiKeyService);
private isBrowser: boolean;
private ngZone: NgZone;
private subscriptions: Subscription[];
@ -27,6 +31,13 @@ export class AppComponent implements OnInit, OnDestroy {
this.ngZone = ngZone;
this.isBrowser = isPlatformBrowser(this.platformId);
this.subscriptions = [];
this.subscriptions.push(this.events.listen('tts_login_ack', async _ => {
await this.router.navigate(['policies'])
}));
this.subscriptions.push(this.events.listen('tts_logoff', async _ => {
await this.router.navigate(['tts-login'])
}));
}
ngOnInit(): void {
@ -47,10 +58,15 @@ export class AppComponent implements OnInit, OnDestroy {
}
}));
this.addSubscription(this.events.listen('login', async (_) => {
if (['/login', '/auth'].some(partial => document.location.href.includes(partial))) {
await this.router.navigate(['tts-login']);
}
this.addSubscription(this.events.listen('login', () => {
this.keyService.fetch(true)
.pipe(timeout(3000), first())
.subscribe(async (d: ApiKey[]) => {
if (d.length > 0)
this.client.login(d[0].id);
else if (['/login', '/auth'].some(partial => document.location.href.includes(partial)))
await this.router.navigate(['tts-login']);
});
}));
this.ngZone.runOutsideAngular(() => setInterval(() => this.client.heartbeat(), 15000));

View File

@ -34,12 +34,8 @@ export class TtsLoginComponent implements OnInit, OnDestroy {
ngOnInit(): void {
this.route.data.subscribe(d => this.api_keys = d['keys']);
this.subscriptions.push(this.events.listen('tts_login_ack', async _ => {
await this.router.navigate(['policies'])
}));
this.subscriptions.push(this.events.listen('tts_logoff', async _ => {
this.selected_api_key = undefined;
await this.router.navigate(['tts-login'])
}));
this.subscriptions.push(this.events.listen('impersonation', _ => {
this.selected_api_key = undefined;
@ -51,8 +47,7 @@ export class TtsLoginComponent implements OnInit, OnDestroy {
}
ngOnDestroy(): void {
if (!this.hermes.logged_in)
this.subscriptions.forEach(s => s.unsubscribe());
this.subscriptions.forEach(s => s.unsubscribe());
}
login() {