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
This commit is contained in:
russell@unturf.com 2026-06-03 17:03:16 -04:00
parent a829c97136
commit 8589c91ca9
No known key found for this signature in database

View file

@ -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