Skip to content

Commit 801310a

Browse files
committed
chore: add browser login auth metrics and refactors
1 parent c179209 commit 801310a

File tree

5 files changed

+46
-18
lines changed

5 files changed

+46
-18
lines changed

src/services/login-browser.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ define(function (require, exports, module) {
151151
PreferencesManager.stateManager.set(PREF_USER_PROFILE_VERSION, crypto.randomUUID());
152152
}
153153

154+
/**
155+
* Calls remote resolveBrowserSession endpoint to verify login status. should not be used frequently.
156+
* @param silentCheck
157+
* @returns {Promise<void>}
158+
* @private
159+
*/
154160
async function _verifyBrowserLogin(silentCheck = false) {
155161
console.log("Verifying browser login status...");
156162

@@ -161,28 +167,36 @@ define(function (require, exports, module) {
161167
isLoggedInUser = true;
162168
ProfileMenu.setLoggedIn(userProfile.profileIcon.initials, userProfile.profileIcon.color);
163169
console.log("Browser login verified for:", userProfile.email);
170+
Metrics.countEvent(Metrics.EVENT_TYPE.AUTH, "browser", "OKLogin");
164171
return;
165172
}
166173

167-
// User is not logged in or error occurred
174+
// User is not logged in or error occurred if here
168175
if(resolveResponse.err === ERR_NOT_LOGGED_IN) {
169176
console.log("No browser session found. Not logged in");
170-
// Only reset UI state if this is not a silent background check
171-
if (!silentCheck) {
172-
_resetBrowserLogin();
173-
} else {
174-
// For silent checks, just update the internal state
175-
isLoggedInUser = false;
176-
userProfile = null;
177-
}
177+
Metrics.countEvent(Metrics.EVENT_TYPE.AUTH, "browser", "NotLoggedIn");
178+
_handleLoginError(silentCheck);
179+
return;
180+
}
181+
182+
if (resolveResponse.err === ERR_INVALID) {
183+
console.log("Invalid auth token, resetting login state");
184+
Metrics.countEvent(Metrics.EVENT_TYPE.AUTH, "browser", "invalidLogin");
185+
_handleLoginError(silentCheck);
178186
return;
179187
}
180188

181189
// Other errors (network, retry later, etc.)
182-
console.log("Browser login verification failed:", resolveResponse.err);
190+
console.log("Browser login verification failed (temporary):", resolveResponse.err);
191+
Metrics.countEvent(Metrics.EVENT_TYPE.AUTH, "browser", "RetryLogin");
192+
// Don't reset login state for temporary errors, regardless of silent check
193+
}
194+
195+
function _handleLoginError(silentCheck) {
183196
if (!silentCheck) {
184197
_resetBrowserLogin();
185198
} else {
199+
// For silent checks, just update the internal state
186200
isLoggedInUser = false;
187201
userProfile = null;
188202
}
@@ -379,13 +393,15 @@ define(function (require, exports, module) {
379393
return;
380394
}
381395

382-
// Always verify login on browser app start (silent check to avoid closing popups)
383-
_verifyBrowserLogin(true).catch(console.error);
396+
// Always verify login on browser app start
397+
_verifyBrowserLogin().catch(console.error);
384398

385399
// Watch for profile changes from other windows/tabs
386400
const pref = PreferencesManager.stateManager.definePreference(PREF_USER_PROFILE_VERSION, 'string', '0');
387401
pref.watchExternalChanges();
388-
pref.on('change', _verifyBrowserLogin);
402+
pref.on('change', ()=>{
403+
_verifyBrowserLogin(true).catch(console.error);
404+
});
389405

390406
// Note: We don't do automatic verification on page focus to avoid server overload.
391407
// Automatic checks are only done during the login waiting dialog period.
@@ -402,7 +418,9 @@ define(function (require, exports, module) {
402418
LoginService.signInToAccount = signInToBrowser;
403419
LoginService.signOutAccount = signOutBrowser;
404420
LoginService.getProfile = getProfile;
405-
LoginService.verifyLoginStatus = () => _verifyBrowserLogin(false);
421+
// verifyLoginStatus Calls remote resolveBrowserSession endpoint to verify. should not be used frequently.
422+
// All users are required to use isLoggedIn API instead.
423+
LoginService._verifyLoginStatus = () => _verifyBrowserLogin(false);
406424
LoginService.getAccountBaseURL = _getAccountBaseURL;
407425
init();
408426
}

src/services/login-desktop.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ define(function (require, exports, module) {
415415
LoginService.signInToAccount = signInToAccount;
416416
LoginService.signOutAccount = signOutAccount;
417417
LoginService.getProfile = getProfile;
418-
LoginService.verifyLoginStatus = () => _verifyLogin(false);
418+
LoginService._verifyLoginStatus = () => _verifyLogin(false);
419419
LoginService.getAccountBaseURL = getAccountBaseURL;
420420
init();
421421
}

src/services/login-service.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,16 @@ define(function (require, exports, module) {
337337
LoginService.clearEntitlements = clearEntitlements;
338338
LoginService.EVENT_ENTITLEMENTS_CHANGED = EVENT_ENTITLEMENTS_CHANGED;
339339

340+
// Test-only exports for integration testing
341+
if (Phoenix.isTestWindow) {
342+
window._test_login_service_exports = {
343+
LoginService,
344+
setFetchFn: function _setFetchFn(fn) {
345+
fetchFn = fn;
346+
}
347+
};
348+
}
349+
340350
// Start the entitlements monitor timer
341351
startEntitlementsMonitor();
342352
});

src/services/profile-menu.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ define(function (require, exports, module) {
541541
// Set flag to indicate this is a background refresh
542542
isBackgroundRefresh = true;
543543

544-
KernalModeTrust.loginService.verifyLoginStatus().then(() => {
544+
KernalModeTrust.loginService._verifyLoginStatus().then(() => {
545545
// Clear the background refresh flag
546546
isBackgroundRefresh = false;
547547

src/services/promotions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ define(function (require, exports, module) {
174174
}
175175

176176
/**
177-
* Check if user has active pro subscription
177+
* Check if user has active pro subscription. this calls actual login endpoint and is not to be used frequently!.
178178
* Returns true if user is logged in and has a paid subscription
179179
*/
180180
async function _hasProSubscription() {
181181
try {
182182
// First verify login status to ensure login state is properly resolved
183-
await LoginService.verifyLoginStatus();
183+
await LoginService._verifyLoginStatus();
184184

185185
// getEntitlements() returns null if not logged in
186186
const entitlements = await LoginService.getEntitlements();

0 commit comments

Comments
 (0)