From 8589c91ca97717c4c274339befaebdda5ed45518 Mon Sep 17 00:00:00 2001 From: "russell@unturf.com" Date: Wed, 3 Jun 2026 17:03:16 -0400 Subject: [PATCH] feat(chat): persist guest username in localStorage across page loads Guests had to retype their username on every page load. Now the entered name is cached in localStorage and reused on subsequent visits. The cache is invalidated by any login/logout transition: - on a page load with a server-provided username (logged in), we clear the cache, so a later logout starts fresh - on a guest page load with no cache, we prompt and save what they type --- templates/chat.html | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/templates/chat.html b/templates/chat.html index 457a41c..e6304c6 100644 --- a/templates/chat.html +++ b/templates/chat.html @@ -129,9 +129,22 @@ const AUDIO_FORMAT = detectAudioFormat(); // Get username from server (authenticated user's display name or None) let username = {% if username %}"{{ username }}"{% else %}null{% endif %}; -// If not authenticated, prompt for username -if (!username) { - username = prompt("Enter your username:", "guest") || "guest"; +// Guest usernames stick in localStorage across page loads so you don't have +// to retype them. The cache is invalidated on any login/logout transition: +// - logging in lands on a page with a server-provided username → we clear it +// - logging out lands on a page with no server username and (after the prior +// clear-on-login) no cache → we prompt fresh +const GUEST_USERNAME_KEY = 'guestUsername'; +if (username) { + localStorage.removeItem(GUEST_USERNAME_KEY); +} else { + const cached = localStorage.getItem(GUEST_USERNAME_KEY); + if (cached) { + username = cached; + } else { + username = prompt("Enter your username:", "guest") || "guest"; + localStorage.setItem(GUEST_USERNAME_KEY, username); + } } // Configuration for DOMPurify to specify which tags and attributes are allowed