Skip to content

Commit

Permalink
fix: redirection to home issue when session is not valid
Browse files Browse the repository at this point in the history
  • Loading branch information
irmastnt committed Jan 18, 2024
1 parent e5fb2fe commit f95978a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
21 changes: 13 additions & 8 deletions libs/vre/shared/app-session/src/lib/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion libs/vre/shared/app-state/src/lib/user/user.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit f95978a

Please sign in to comment.