Skip to content

Commit 950beb3

Browse files
authored
Merge pull request #25 from faztp12/observe-ls-session
feat: Observe LocalStorage Session for Cross Tab Session Updates
2 parents ba1660f + 6a88e46 commit 950beb3

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

src/auth/auth.controller.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,8 @@ export default abstract class AuthController extends RenovationController {
8989
});
9090
}
9191
});
92-
const session = this.getSessionFromLocalStorage();
93-
this.updateSession({
94-
data: session,
95-
loggedIn: !!session && !!session.loggedIn,
96-
// its mandatory not to update timestamp on loading from local storage
97-
// this causes checkLogin() to verify with backend
98-
// otherwise, it will return logged in
99-
useTimestamp: !!session && session.timestamp
100-
});
92+
this.updateSessionFromLocalStorage();
93+
this.observerLocalStorageSession();
10194
}
10295

10396
/**
@@ -403,6 +396,36 @@ export default abstract class AuthController extends RenovationController {
403396
}
404397
}
405398

399+
/**
400+
* Loads the Session details from LocalStorage and updates current Session
401+
*/
402+
private updateSessionFromLocalStorage() {
403+
const session = this.getSessionFromLocalStorage();
404+
this.updateSession({
405+
data: session,
406+
loggedIn: !!session && !!session.loggedIn,
407+
// its mandatory not to update timestamp on loading from local storage
408+
// this causes checkLogin() to verify with backend
409+
// otherwise, it will return logged in
410+
useTimestamp: !!session && session.timestamp
411+
});
412+
}
413+
414+
/**
415+
* Adds EventListener for LocalStorage changes
416+
* This helps in catching SessionUpdates in different browser Tabs
417+
*/
418+
private observerLocalStorageSession() {
419+
if (!isBrowser()) {
420+
return;
421+
}
422+
window.addEventListener("storage", e => {
423+
if (e.key == renovationSessionKey) {
424+
this.updateSessionFromLocalStorage();
425+
}
426+
});
427+
}
428+
406429
/**
407430
* Sets the user's language in Frappé backend.
408431
*

0 commit comments

Comments
 (0)