diff --git a/src/app_helpers.py b/src/app_helpers.py index 1b915a5a2..d3c831d47 100644 --- a/src/app_helpers.py +++ b/src/app_helpers.py @@ -1,5 +1,6 @@ # src/app_helpers.py import base64 +import json import logging import os @@ -9,6 +10,37 @@ from starlette.requests import Request logger = logging.getLogger(__name__) + +def _login_theme_json() -> str: + """Active profile theme as a script-safe JSON literal for the login page. + + The pre-auth login page can't fetch /api/prefs/theme (401) and a fresh + device has nothing in localStorage, so without this it always renders the + built-in default. We inject the profile's active theme so login matches + the theme set in-app. Only a single-user instance is handled — with more + than one user there's no way to know whose theme to show before sign-in, + so we return "null" and let the client fall back to localStorage/default. + Returns the string "null" (a valid JS literal) on any miss or error. + """ + try: + from src.constants import USER_PREFS_FILE + with open(USER_PREFS_FILE, "r", encoding="utf-8") as f: + users = (json.load(f) or {}).get("_users", {}) + if len(users) != 1: + return "null" + theme = next(iter(users.values())).get("theme") + if not isinstance(theme, dict) or not theme.get("colors"): + return "null" + # Escape so the JSON can't break out of the surrounding