diff --git a/apps/red-ui/src/app/guards/if-logged-in.guard.ts b/apps/red-ui/src/app/guards/if-logged-in.guard.ts index 51c74a392..39a127622 100644 --- a/apps/red-ui/src/app/guards/if-logged-in.guard.ts +++ b/apps/red-ui/src/app/guards/if-logged-in.guard.ts @@ -5,8 +5,12 @@ import { keycloakInitializer, KeycloakStatusService, TenantsService } from '@iqs import { KeycloakService } from 'keycloak-angular'; import { UserService } from '@users/user.service'; import { LicenseService } from '@services/license.service'; -import { RouterHistoryService } from '@services/router-history.service'; import { AsyncGuard } from '@iqser/common-ui'; +import jwt_decode from 'jwt-decode'; + +export interface JwtToken { + auth_time: number; +} export function ifLoggedIn(): AsyncGuard { return async (route: ActivatedRouteSnapshot) => { @@ -18,7 +22,6 @@ export function ifLoggedIn(): AsyncGuard { const usersService = inject(UserService); const licenseService = inject(LicenseService); const keycloakStatusService = inject(KeycloakStatusService); - const routerHistoryService = inject(RouterHistoryService); const keycloakInstance = keycloakService.getKeycloakInstance(); const tenant = route.paramMap.get('tenant'); @@ -37,7 +40,10 @@ export function ifLoggedIn(): AsyncGuard { await tenantsService.selectTenant(tenant); await usersService.initialize(); await licenseService.loadLicenses(); - routerHistoryService.clearRouterHistory(); + + const token = await keycloakService.getToken(); + const authTime = (jwt_decode(token) as JwtToken).auth_time.toString(); + localStorage.setItem('authTime', authTime); } const isLoggedIn = await keycloakService.isLoggedIn(); diff --git a/apps/red-ui/src/app/modules/dashboard/components/template-stats/template-stats.component.html b/apps/red-ui/src/app/modules/dashboard/components/template-stats/template-stats.component.html index 7c479b1dc..e0719929d 100644 --- a/apps/red-ui/src/app/modules/dashboard/components/template-stats/template-stats.component.html +++ b/apps/red-ui/src/app/modules/dashboard/components/template-stats/template-stats.component.html @@ -1,7 +1,7 @@ diff --git a/apps/red-ui/src/app/services/router-history.service.ts b/apps/red-ui/src/app/services/router-history.service.ts index 65cac7491..7c350218c 100644 --- a/apps/red-ui/src/app/services/router-history.service.ts +++ b/apps/red-ui/src/app/services/router-history.service.ts @@ -2,6 +2,9 @@ import { Injectable } from '@angular/core'; import { NavigationEnd, Router } from '@angular/router'; import { filter } from 'rxjs/operators'; import { TenantsService } from '@iqser/common-ui/lib/tenants'; +import jwt_decode from 'jwt-decode'; +import { KeycloakService } from 'keycloak-angular'; +import { JwtToken } from '@guards/if-logged-in.guard'; const LAST_DOSSIERS_SCREEN = 'routerHistory_lastDossiersScreen'; @@ -9,32 +12,48 @@ const LAST_DOSSIERS_SCREEN = 'routerHistory_lastDossiersScreen'; providedIn: 'root', }) export class RouterHistoryService { - private _lastDossiersScreen = localStorage.getItem(LAST_DOSSIERS_SCREEN); - constructor( private readonly _router: Router, private readonly _tenantsService: TenantsService, + private readonly _keycloakService: KeycloakService, ) { // eslint-disable-next-line rxjs/no-ignored-subscription this._router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe((event: NavigationEnd) => { if (event.url.includes('/dossiers') || event.url.includes('/archive')) { - this._lastDossiersScreen = event.url; - localStorage.setItem(LAST_DOSSIERS_SCREEN, this._lastDossiersScreen); + const lastDossiersScreen = event.url; + localStorage.setItem(LAST_DOSSIERS_SCREEN, lastDossiersScreen); + } + if (event.urlAfterRedirects.includes('/dashboard')) { + localStorage.removeItem(LAST_DOSSIERS_SCREEN); } }); + this.#clearRouterHistory(); } navigateToLastDossiersScreen(): void { - if (this._router.url === decodeURI(this._lastDossiersScreen)) { + const lastDossiersScreen = localStorage.getItem(LAST_DOSSIERS_SCREEN); + if (this._router.url === decodeURI(lastDossiersScreen) || lastDossiersScreen === null) { this._router.navigate(['/' + this._tenantsService.activeTenantId]); } else { - const url = decodeURI(this._lastDossiersScreen).split('?')[0]; + const url = decodeURI(lastDossiersScreen).split('?')[0]; // todo links this._router.navigate([url]); } } - clearRouterHistory() { - localStorage.removeItem(LAST_DOSSIERS_SCREEN); + #clearRouterHistory() { + const interval = window.setInterval(async () => { + if (this._tenantsService.activeTenantId) { + const token = await this._keycloakService.getToken(); + const authTime = (jwt_decode(token) as JwtToken).auth_time; + const localStorageAuthTime = localStorage.getItem('authTime'); + + if (authTime.toString() !== localStorageAuthTime) { + localStorage.removeItem(LAST_DOSSIERS_SCREEN); + } + + window.clearInterval(interval); + } + }, 100); } }