From cfd1f4dbca0dcb9c6f1bb487f60fc9cf47306ad4 Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Sat, 18 Oct 2025 21:11:38 -0400 Subject: [PATCH] Ensure logout flow handles handled flag --- js/app.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 69 insertions(+), 9 deletions(-) diff --git a/js/app.js b/js/app.js index e6ed41ca..7628279d 100644 --- a/js/app.js +++ b/js/app.js @@ -339,17 +339,50 @@ class Application { logger: devLogger, }); this.authEventUnsubscribes.push( - this.authService.on("auth:login", (detail) => this.handleAuthLogin(detail)) + this.authService.on("auth:login", (detail) => { + const maybePromise = this.handleAuthLogin(detail); + if (maybePromise && typeof maybePromise.then === "function") { + maybePromise.catch((error) => { + devLogger.error("Failed to process auth login event:", error); + }); + } + }) ); this.authEventUnsubscribes.push( - this.authService.on("auth:logout", (detail) => - this.handleAuthLogout(detail) - ) + this.authService.on("auth:logout", (detail) => { + if (detail && typeof detail === "object") { + if (detail.__handled === true) { + return; + } + try { + detail.__handled = true; + } catch (error) { + devLogger.warn( + "Failed to mark auth logout detail as handled:", + error, + ); + } + } + + const maybePromise = this.handleAuthLogout(detail); + if (maybePromise && typeof maybePromise.then === "function") { + maybePromise.catch((error) => { + devLogger.error("Failed to process auth logout event:", error); + }); + } + }) ); this.authEventUnsubscribes.push( - this.authService.on("profile:updated", (detail) => - this.handleProfileUpdated(detail) - ) + this.authService.on("profile:updated", (detail) => { + try { + this.handleProfileUpdated(detail); + } catch (error) { + devLogger.warn( + "Failed to process profile update event:", + error, + ); + } + }) ); // Optional: a "profile" button or avatar (if used) @@ -504,7 +537,7 @@ class Application { const profileModalCallbacks = { onClose: () => this.handleProfileModalClosed(), - onLogout: async () => this.authService.logout(), + onLogout: async () => this.requestLogout(), onChannelLink: (element) => this.handleProfileChannelLink(element), onAddAccount: (controller) => this.handleAddProfile(controller), onRequestSwitchProfile: (payload) => @@ -2004,7 +2037,7 @@ class Application { if (this.logoutButton) { this.logoutButton.addEventListener("click", async () => { try { - await this.authService.logout(); + await this.requestLogout(); } catch (error) { devLogger.error("Logout failed:", error); this.showError("Failed to logout. Please try again."); @@ -3264,7 +3297,34 @@ class Application { } } + async requestLogout() { + const detail = await this.authService.logout(); + + if (detail && typeof detail === "object") { + if (detail.__handled === true) { + return detail; + } + + try { + detail.__handled = true; + } catch (error) { + devLogger.warn("Failed to mark logout detail as handled:", error); + } + } + + await this.handleAuthLogout(detail); + return detail ?? null; + } + async handleAuthLogout(detail = {}) { + if (detail && typeof detail === "object") { + try { + detail.__handled = true; + } catch (error) { + devLogger.warn("Failed to mark logout detail as handled:", error); + } + } + this.resetViewLoggingState(); this.pendingModalZapOpen = false;