2025-01-09 18:11:44 +00:00
|
|
|
import { isPlatformBrowser } from '@angular/common';
|
2024-10-17 08:48:15 +00:00
|
|
|
import { Component, OnInit, Inject, PLATFORM_ID, NgZone, OnDestroy } from '@angular/core';
|
2024-10-25 19:09:34 +00:00
|
|
|
import { Router, RouterOutlet } from '@angular/router';
|
2024-10-17 08:48:15 +00:00
|
|
|
import { HermesClientService } from './hermes-client.service';
|
2024-10-31 05:33:11 +00:00
|
|
|
import { AuthUserGuard } from './shared/auth/auth.user.guard'
|
2024-10-17 08:48:15 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
|
|
|
import { NavigationComponent } from "./navigation/navigation.component";
|
|
|
|
import EventService from './shared/services/EventService';
|
|
|
|
import { ApiAuthenticationService } from './shared/services/api/api-authentication.service';
|
2025-01-07 17:56:11 +00:00
|
|
|
import { AuthModule } from './auth/auth.module';
|
2024-07-06 01:01:52 +00:00
|
|
|
|
|
|
|
@Component({
|
2025-01-09 18:11:44 +00:00
|
|
|
selector: 'app-root',
|
|
|
|
standalone: true,
|
|
|
|
imports: [RouterOutlet, AuthModule, NavigationComponent],
|
|
|
|
providers: [AuthUserGuard],
|
|
|
|
templateUrl: './app.component.html',
|
|
|
|
styleUrl: './app.component.scss'
|
2024-07-06 01:01:52 +00:00
|
|
|
})
|
2024-10-17 08:48:15 +00:00
|
|
|
export class AppComponent implements OnInit, OnDestroy {
|
2025-01-09 18:11:44 +00:00
|
|
|
private isBrowser: boolean;
|
|
|
|
private ngZone: NgZone;
|
|
|
|
private subscriptions: Subscription[];
|
2024-10-17 08:48:15 +00:00
|
|
|
|
|
|
|
|
2025-01-09 18:11:44 +00:00
|
|
|
constructor(private auth: ApiAuthenticationService, private client: HermesClientService, private events: EventService, private router: Router, ngZone: NgZone, @Inject(PLATFORM_ID) private platformId: Object) {
|
|
|
|
this.ngZone = ngZone;
|
|
|
|
this.isBrowser = isPlatformBrowser(this.platformId);
|
|
|
|
this.subscriptions = [];
|
|
|
|
}
|
2024-10-17 08:48:15 +00:00
|
|
|
|
2025-01-09 18:11:44 +00:00
|
|
|
ngOnInit(): void {
|
|
|
|
if (!this.isBrowser)
|
|
|
|
return;
|
2024-10-17 08:48:15 +00:00
|
|
|
|
2025-01-09 18:11:44 +00:00
|
|
|
this.auth.update();
|
2024-10-17 08:48:15 +00:00
|
|
|
|
2025-01-09 18:11:44 +00:00
|
|
|
this.addSubscription(this.events.listen('logoff', async (message) => {
|
|
|
|
localStorage.removeItem('jwt');
|
|
|
|
if (!document.location.href.includes('/login')) {
|
|
|
|
if (message)
|
|
|
|
await this.router.navigate(['login'], {
|
|
|
|
queryParams: { message: message }
|
|
|
|
});
|
|
|
|
else
|
|
|
|
await this.router.navigate(['login']);
|
|
|
|
}
|
|
|
|
}));
|
2024-10-25 19:09:34 +00:00
|
|
|
|
2025-01-09 18:11:44 +00:00
|
|
|
this.addSubscription(this.events.listen('login', async (_) => {
|
|
|
|
if (['/login', '/auth'].some(partial => document.location.href.includes(partial))) {
|
|
|
|
await this.router.navigate(['tts-login']);
|
|
|
|
}
|
|
|
|
}));
|
2024-10-25 19:09:34 +00:00
|
|
|
|
2025-01-09 18:11:44 +00:00
|
|
|
this.ngZone.runOutsideAngular(() => setInterval(() => this.client.heartbeat(), 15000));
|
|
|
|
}
|
2024-10-17 08:48:15 +00:00
|
|
|
|
2025-01-09 18:11:44 +00:00
|
|
|
ngOnDestroy() {
|
|
|
|
for (let s of this.subscriptions) {
|
|
|
|
s.unsubscribe();
|
2024-10-25 19:09:34 +00:00
|
|
|
}
|
2025-01-09 18:11:44 +00:00
|
|
|
}
|
2024-10-25 19:09:34 +00:00
|
|
|
|
2025-01-09 18:11:44 +00:00
|
|
|
private addSubscription(s: Subscription) {
|
|
|
|
this.subscriptions.push(s);
|
|
|
|
}
|
2024-10-17 08:48:15 +00:00
|
|
|
}
|