From f95978a9bc78973ba7b0db6678a722f8f566c48e Mon Sep 17 00:00:00 2001 From: IrKa Date: Thu, 18 Jan 2024 15:50:35 +0100 Subject: [PATCH] fix: redirection to home issue when session is not valid --- .../app-session/src/lib/auth.service.ts | 21 ++++++++++++------- .../app-state/src/lib/user/user.selectors.ts | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/libs/vre/shared/app-session/src/lib/auth.service.ts b/libs/vre/shared/app-session/src/lib/auth.service.ts index 6ae5cdf548..020a18f024 100644 --- a/libs/vre/shared/app-session/src/lib/auth.service.ts +++ b/libs/vre/shared/app-session/src/lib/auth.service.ts @@ -7,6 +7,7 @@ import { ClearOntologiesAction, ClearProjectsAction, LogUserOutAction, + UserSelectors, } from '@dasch-swiss/vre/shared/app-state'; import { Actions, Store, ofActionSuccessful } from '@ngxs/store'; import jwt_decode, { JwtPayload } from 'jwt-decode'; @@ -31,13 +32,7 @@ export class AuthService { private router: Router // private intervalWrapper: IntervalWrapperService ) { // check if the (possibly) existing session is still valid and if not, destroy it - this.isSessionValid$() - .pipe(takeLast(1)) - .subscribe(valid => { - if (!valid) { - this.doLogoutUser(); - } - }); + this.isSessionValid$(); // if (this.isLoggedIn()) { // this.startTokenRefresh(); @@ -78,6 +73,12 @@ export class AuthService { } else { // no session found; update knora api connection with empty jwt this._dspApiConnection.v2.jsonWebToken = ''; + + const username = this.store.selectSnapshot(UserSelectors.username); + if (username) { + this.clearState(); + } + if (forceLogout) { this.doLogoutUser(); } @@ -185,13 +186,17 @@ export class AuthService { .navigate([RouteConstants.logout], { skipLocationChange: true }) .then(() => this.router.navigate([RouteConstants.home])) ); + this.clearState(); + clearTimeout(this.tokenRefreshIntervalId); + } + + clearState() { this.store.dispatch([ new LogUserOutAction(), new ClearProjectsAction(), new ClearListsAction(), new ClearOntologiesAction(), ]); - clearTimeout(this.tokenRefreshIntervalId); } isLoggedIn() { diff --git a/libs/vre/shared/app-state/src/lib/user/user.selectors.ts b/libs/vre/shared/app-state/src/lib/user/user.selectors.ts index 2497dfb97b..17d6269062 100644 --- a/libs/vre/shared/app-state/src/lib/user/user.selectors.ts +++ b/libs/vre/shared/app-state/src/lib/user/user.selectors.ts @@ -40,7 +40,7 @@ export class UserSelectors { @Selector([UserState]) static username(state: UserStateModel): string | null | undefined { - return state.user?.username; + return state?.user?.username; } @Selector([UserState])