Skip to content

Commit 949c4bc

Browse files
committed
feat: multi window signin signout working
1 parent 1741a67 commit 949c4bc

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/services/login.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,16 @@ define(function (require, exports, module) {
9898
isLoggedInUser = false;
9999
ProfileMenu.setNotLoggedIn();
100100
await KernalModeTrust.removeCredential(KernalModeTrust.CRED_KEY_API);
101+
// bump the version so that in multi windows, the other window gets notified of the change
102+
PreferencesManager.stateManager.set(PREF_USER_PROFILE_VERSION, crypto.randomUUID());
101103
}
102104

103105
async function _verifyLogin() {
104106
const savedUserProfile = await KernalModeTrust.getCredential(KernalModeTrust.CRED_KEY_API);
105107
if(!savedUserProfile){
106108
console.log("No savedUserProfile found. Not logged in");
107109
ProfileMenu.setNotLoggedIn();
110+
isLoggedInUser = false;
108111
return;
109112
}
110113
try {
@@ -124,6 +127,8 @@ define(function (require, exports, module) {
124127
userProfile = resolveResponse.userDetails;
125128
ProfileMenu.setLoggedIn(userProfile.profileIcon.initials, userProfile.profileIcon.color);
126129
await KernalModeTrust.setCredential(KernalModeTrust.CRED_KEY_API, JSON.stringify(userProfile));
130+
// we dont need to bump the PREF_USER_PROFILE_VERSION here as its just a cred update
131+
// (maybe name) and may lead to infi loops.
127132
return;
128133
}
129134
// some error happened.
@@ -235,6 +240,8 @@ define(function (require, exports, module) {
235240
userProfile = resolveResponse.userDetails;
236241
ProfileMenu.setLoggedIn(userProfile.profileIcon.initials, userProfile.profileIcon.color);
237242
await KernalModeTrust.setCredential(KernalModeTrust.CRED_KEY_API, JSON.stringify(userProfile));
243+
// bump the version so that in multi windows, the other window gets notified of the change
244+
PreferencesManager.stateManager.set(PREF_USER_PROFILE_VERSION, crypto.randomUUID());
238245
checkAgain = false;
239246
isLoggedInUser = true;
240247
dialog.close();
@@ -309,9 +316,9 @@ define(function (require, exports, module) {
309316
return;
310317
}
311318
_verifyLogin().catch(console.error);// todo raise metrics
312-
const pref = PreferencesManager.stateManager.definePreference(PREF_USER_PROFILE_VERSION, 'string', '0')
313-
.watchExternalChanges();
314-
319+
const pref = PreferencesManager.stateManager.definePreference(PREF_USER_PROFILE_VERSION, 'string', '0');
320+
pref.watchExternalChanges();
321+
pref.on('change', _verifyLogin);
315322
}
316323

317324
init();

0 commit comments

Comments
 (0)