From 1dab2f7f4e35e5377f46b23352ff7b9c020df31d Mon Sep 17 00:00:00 2001 From: kritzl Date: Thu, 14 May 2026 18:11:26 +0200 Subject: [PATCH] language switcher, auto refresh auth session --- app/src/assets/main.ts | 20 ++++++++++++++++++-- app/src/i18n/ui.ts | 2 ++ app/src/layouts/Layout.astro | 24 ++++++++++++++++-------- app/src/pages/[lang]/index.astro | 2 +- 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/app/src/assets/main.ts b/app/src/assets/main.ts index 9bab4b4..5f5b5e3 100644 --- a/app/src/assets/main.ts +++ b/app/src/assets/main.ts @@ -45,9 +45,19 @@ const loading: { auth: false, } +const timeouts: Record = { + auth: null, +} const doors: Array = [] +function triggerAuthTimeout() { + const diff = auth.until ? (auth.until.getTime() - new Date().getTime()) : 0 + + if (timeouts.auth) clearTimeout(timeouts.auth) + timeouts.auth = setTimeout(checkUser, diff) +} + function loadAuthFromLocalStorage() { if (localStorage.getItem("auth")) { const localAuth = JSON.parse(localStorage.getItem("auth")!) @@ -60,7 +70,10 @@ function loadAuthFromLocalStorage() { } async function checkUser() { - loading.auth = true + if (!auth.authenticated) { + loading.auth = true + } + const getUserInfo = fetcher.path("/api/user-info/").method("get").create() try { @@ -72,6 +85,8 @@ async function checkUser() { auth.until = userInfo.guaranteed_session_until ? new Date(userInfo.guaranteed_session_until) : null auth.username = userInfo.username ?? "" auth.recentLogout = false + + triggerAuthTimeout() } catch (e) { // check which operation threw the exception if (e instanceof getUserInfo.Error) { @@ -142,6 +157,7 @@ async function fetchDoors() { console.log("unauthorized") } else if (error.status >= 500 && error.status < 600) { apiError.current = "serverError" + clearInterval(doorsInterval) } else { console.error("unknown error:", error) } @@ -262,7 +278,7 @@ function refresh() { loadAuthFromLocalStorage() -setInterval(fetchDoors, 250) // TODO: replace with SSE +const doorsInterval = setInterval(fetchDoors, 250) // TODO: replace with SSE checkUser() document.addEventListener("loadeddata", () => { diff --git a/app/src/i18n/ui.ts b/app/src/i18n/ui.ts index c6be0f4..b8d9c28 100644 --- a/app/src/i18n/ui.ts +++ b/app/src/i18n/ui.ts @@ -32,6 +32,7 @@ export const ui = { "serverError.description": `Please try again later.`, "networkError.title": "Network error", "networkError.description": `Please check your network connection.`, + "loadingDoors": 'Loading doors', }, de: { "dooris": "TüRIS (DOORIS)", @@ -59,5 +60,6 @@ export const ui = { "serverError.description": `Bitte versuche es später erneut.`, "networkError.title": "Netzwerkfehler", "networkError.description": `Bitte überprüfe deine Internetverbindung.`, + "loadingDoors": 'Lade Türen', }, } as const \ No newline at end of file diff --git a/app/src/layouts/Layout.astro b/app/src/layouts/Layout.astro index 35acad7..c2badc1 100644 --- a/app/src/layouts/Layout.astro +++ b/app/src/layouts/Layout.astro @@ -1,6 +1,7 @@ --- import {UserRound, LogIn, DoorOpen} from "@lucide/astro" import {getLangFromUrl, useTranslations} from "../i18n/utils" +import {languages} from "../i18n/ui.ts" const lang = getLangFromUrl(Astro.url) const t = useTranslations(lang) @@ -23,16 +24,23 @@ const t = useTranslations(lang)

- +

- diff --git a/app/src/pages/[lang]/index.astro b/app/src/pages/[lang]/index.astro index d4af8b5..b4cf578 100644 --- a/app/src/pages/[lang]/index.astro +++ b/app/src/pages/[lang]/index.astro @@ -76,7 +76,7 @@ const t = useTranslations(lang)