From 41e3f0136fa8cfe4f55a0e0177e923251ede37bf Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Mon, 21 Jul 2025 20:28:19 +0200 Subject: [PATCH] chore(webui): improve auth state management --- .../custom/deleteAccountController.ts | 4 ++++ static/webui/script.js | 17 ++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/controllers/custom/deleteAccountController.ts b/src/controllers/custom/deleteAccountController.ts index 449a44c4..abe33315 100644 --- a/src/controllers/custom/deleteAccountController.ts +++ b/src/controllers/custom/deleteAccountController.ts @@ -11,6 +11,7 @@ import { GuildMember } from "@/src/models/guildModel"; import { Leaderboard } from "@/src/models/leaderboardModel"; import { deleteGuild } from "@/src/services/guildService"; import { Friendship } from "@/src/models/friendModel"; +import { sendWsBroadcastTo } from "@/src/services/wsService"; export const deleteAccountController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); @@ -36,5 +37,8 @@ export const deleteAccountController: RequestHandler = async (req, res) => { Ship.deleteMany({ ShipOwnerId: accountId }), Stats.deleteOne({ accountOwnerId: accountId }) ]); + + sendWsBroadcastTo(accountId, { logged_out: true }); + res.end(); }; diff --git a/static/webui/script.js b/static/webui/script.js index 6ab0fe65..aa882ccc 100644 --- a/static/webui/script.js +++ b/static/webui/script.js @@ -118,9 +118,16 @@ function doLogin() { window.registerSubmit = false; } -async function revalidateAuthz() { - await getWebSocket(); - // We have a websocket connection, so authz should be good. +function revalidateAuthz() { + return new Promise(resolve => { + let interval; + interval = setInterval(() => { + if (ws_is_open && !auth_pending) { + clearInterval(interval); + resolve(); + } + }, 10); + }); } function logout() { @@ -2080,6 +2087,10 @@ single.getRoute("/webui/cheats").on("beforeload", function () { }) .fail(res => { if (res.responseText == "Log-in expired") { + if (ws_is_open && !auth_pending) { + console.warn("Credentials invalidated but the server didn't let us know"); + sendAuth(); + } revalidateAuthz().then(() => { if (single.getCurrentPath() == "/webui/cheats") { single.loadRoute("/webui/cheats");