diff --git a/CHANGELOG.md b/CHANGELOG.md index 7172050c..cb0c255e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,8 @@ ### Minor changes and fixes -* Anonymous chat user: remember the chosen nickname in sessionStorage, to avoid entering it again too often. -* Fix: if an anonymous chat user enter spaces in the nickname choice, it will allows them to keep the random nickname. +* Anonymous chat user: remember the chosen nickname in localStorage, to avoid entering it again too often. Nickname will expire after 12 hours. +* Fix: if an anonymous chat user enter spaces in the nickname choice, it allowed them to keep the random nickname. * Authenticated users: if current user nickname is already used in the room, automatically add a suffix. * UX: add a label ('Choose a nickname to enter') for the anonymous nickname prompt. Fix #287. * Translation updates: German, French. diff --git a/conversejs/lib/nick.ts b/conversejs/lib/nick.ts index d8e3b9f3..5eeb7b48 100644 --- a/conversejs/lib/nick.ts +++ b/conversejs/lib/nick.ts @@ -10,12 +10,28 @@ function randomNick (base: string): string { } /** - * Get the previous anonymous nickname (stored in sessionStorage). + * Get the previous anonymous nickname (stored in localStorage). + * If it was set more than 12 hours ago, it will be ignored (considering the nickname should not remain forever). * @returns previous nickname or null */ function getPreviousAnonymousNick (): string | null { try { - return sessionStorage.getItem('livechat-previous-anonymous-nickname') + const lastAccess = parseInt(localStorage.getItem('livechat-previous-anonymous-nickname-last-access') ?? '0') + const now = Date.now() + if (lastAccess && !isNaN(lastAccess)) { + const expires = lastAccess + 1000 * 60 * 60 * 12 // nickname expires after 12 hours + if (now > expires) { + console.log('Anonymous nickname has expired.') + localStorage.removeItem('livechat-previous-anonymous-nickname') + localStorage.removeItem('livechat-previous-anonymous-nickname-last-access') + return null + } + } + const nick = localStorage.getItem('livechat-previous-anonymous-nickname') + if (nick !== null) { + localStorage.setItem('livechat-previous-anonymous-nickname-last-access', now.toString()) + } + return nick } catch (err) { console.error(err) return null @@ -23,12 +39,13 @@ function getPreviousAnonymousNick (): string | null { } /** - * Stores the chosen nickname in sessionStorage. + * Stores the chosen nickname in localStorage. */ function setPreviousAnonymousNick (nick: string): void { try { console.log('Storing anonymous nickname', nick) - sessionStorage.setItem('livechat-previous-anonymous-nickname', nick) + localStorage.setItem('livechat-previous-anonymous-nickname', nick) + localStorage.setItem('livechat-previous-anonymous-nickname-last-access', Date.now().toString()) } catch (err) { console.error(err) }