Skip to content

Commit 316df47

Browse files
committed
feat: close session expired dialog in all tabs
The reauthentication of user in multiple tabs works fine. However the session expired dialog stayed present aand prevented usage of the app. This is no longer the case
1 parent ca99c2b commit 316df47

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

libs/perun/services/src/lib/ApiInterceptor.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,23 @@ export class ApiInterceptor implements HttpInterceptor {
5656
) {
5757
const config = getDefaultDialogConfig();
5858
config.width = '450px';
59+
config.id = 'SessionExpirationDialog';
5960

6061
// Skip if the dialog is already open
61-
if (this.dialogRefSessionExpiration == null) {
62+
if (!this.dialogRefSessionExpiration) {
6263
this.dialogRefSessionExpiration = this.dialog.open(
6364
SessionExpirationDialogComponent,
6465
config
6566
);
66-
this.dialogRefSessionExpiration.afterClosed().subscribe(() => {
67-
finalize(() => (this.dialogRefSessionExpiration = undefined));
68-
sessionStorage.setItem('auth:redirect', location.pathname);
69-
sessionStorage.setItem('auth:queryParams', location.search.substring(1));
70-
this.dialog.closeAll();
71-
this.oauthService.logOut(true);
72-
this.reauthenticate();
67+
this.dialogRefSessionExpiration.afterClosed().subscribe((manuallyClosed: boolean) => {
68+
finalize(() => (this.dialogRefSessionExpiration = null));
69+
// Only for the case when user closed the dialog manually to authenticate
70+
// it can be automatically closed in other tabs once user refreshes session
71+
if (manuallyClosed) {
72+
this.dialog.closeAll();
73+
this.oauthService.logOut(true);
74+
this.reauthenticate();
75+
}
7376
});
7477
}
7578
}

libs/perun/services/src/lib/auth.service.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,22 @@ export class AuthService {
3333
this.filterShortname = String(params['idpFilter']);
3434
}
3535
});
36+
37+
// The storage event of the Window interface fires when a storage area (localStorage) has been modified in the context of another document.
38+
window.addEventListener('storage', this.closeSessionDialogsForOtherTabs);
3639
}
3740

41+
closeSessionDialogsForOtherTabs = (event: StorageEvent): void => {
42+
// Check if user authenticated in other tab and if so close the session expiration dialog
43+
if (event.key === 'access_token' && this.oauthService.hasValidAccessToken()) {
44+
this.dialog.openDialogs.forEach((dialog) => {
45+
if (dialog.id === 'SessionExpirationDialog') {
46+
dialog.close();
47+
}
48+
});
49+
}
50+
};
51+
3852
loadOidcConfigData(): void {
3953
this.oauthService.configure(this.getClientConfig());
4054
}

0 commit comments

Comments
 (0)