fix(theme): make preference state deterministic

This commit is contained in:
cesnimda
2026-08-09 18:35:31 +02:00
parent 378807b3b9
commit 11734ee058
8 changed files with 240 additions and 23 deletions
+4
View File
@@ -2,6 +2,7 @@ export const AUTH_REMEMBER_ME_KEY = "authRememberMe";
const AUTH_PERSISTENCE_KEY = "authTokenPersistence";
const AUTH_USER_KEY = "authUserKey";
const AUTH_CSRF_COOKIE = "XSRF-TOKEN";
export const AUTH_USER_CHANGED_EVENT = "auth-user-changed";
export type AuthPersistence = "local" | "session";
@@ -72,12 +73,14 @@ export function getAuthUserKey(): string {
}
export function setAuthUserKey(value: string | null | undefined, emit = true) {
const previous = getAuthUserKey();
const next = typeof value === "string" ? value.trim() : "";
if (!next) {
safeRemove(window.localStorage, AUTH_USER_KEY);
} else {
safeSet(window.localStorage, AUTH_USER_KEY, next);
}
if (previous !== (next || "anon")) window.dispatchEvent(new Event(AUTH_USER_CHANGED_EVENT));
if (emit) emitAuthChanged();
}
@@ -89,6 +92,7 @@ export function clearAuthClientState(emit = true) {
// the user is logged out (login page, expired session).
const had = safeGet(window.localStorage, AUTH_USER_KEY) != null;
safeRemove(window.localStorage, AUTH_USER_KEY);
if (had) window.dispatchEvent(new Event(AUTH_USER_CHANGED_EVENT));
if (emit && had) emitAuthChanged();
}