language switcher, auto refresh auth session
All checks were successful
Build Container / Build Container (push) Successful in 34s
All checks were successful
Build Container / Build Container (push) Successful in 34s
This commit is contained in:
parent
26357370cd
commit
1dab2f7f4e
4 changed files with 37 additions and 11 deletions
|
|
@ -45,9 +45,19 @@ const loading: {
|
|||
auth: false,
|
||||
}
|
||||
|
||||
const timeouts: Record<string, number | null> = {
|
||||
auth: null,
|
||||
}
|
||||
|
||||
const doors: Array<DoorType> = []
|
||||
|
||||
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", () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue