check if user is logged in

This commit is contained in:
kritzl 2026-05-10 21:14:20 +02:00
commit 2a65cd9f06
Signed by: kritzl
SSH key fingerprint: SHA256:5BmINP9VjZWaUk5Z+2CTut1KFhwLtd0ZynMekKbtViM

View file

@ -48,7 +48,10 @@ const t = useTranslations(lang)
<DoorTemplate state="unknown"/> <DoorTemplate state="unknown"/>
<script> <script>
import user from "@lucide/astro/icons/user" import {Fetcher} from "openapi-typescript-fetch"
import {type paths} from "../../api/schema"
const fetcher = Fetcher.for<paths>()
const list: HTMLDivElement = document.querySelector("#list")! const list: HTMLDivElement = document.querySelector("#list")!
const template: HTMLTemplateElement = document.querySelector("#template-door")! const template: HTMLTemplateElement = document.querySelector("#template-door")!
@ -63,12 +66,13 @@ const t = useTranslations(lang)
username: string; username: string;
authorized: boolean; authorized: boolean;
authenticated: boolean; authenticated: boolean;
until: Date | null;
} }
const auth: AuthType = { const auth: AuthType = {
username: "user", username: "user",
authorized: true, authorized: true,
authenticated: false, authenticated: false,
until: null,
} }
const doors: Array<DoorType> = [ const doors: Array<DoorType> = [
@ -86,6 +90,37 @@ const t = useTranslations(lang)
}, },
] ]
async function checkUser() {
const getUserInfo = fetcher.path("/api/user-info/").method("get").create()
try {
const {status, data: userInfo} = await getUserInfo({
status: ["available", "pending"],
})
auth.authenticated = userInfo.is_logged_in
auth.authorized = false
auth.until = userInfo.guaranteed_session_until ? new Date(userInfo.guaranteed_session_until) : null
auth.username = userInfo.user_info?.username ?? ''
} catch (e) {
// check which operation threw the exception
if (e instanceof getUserInfo.Error) {
const error = e.getActualType()
if (error.status === 401) {
auth.authenticated = false
auth.authorized = false
auth.until = null
auth.username = ''
} else {
console.log('unknown error')
}
}
} finally {
refresh()
}
}
function setDoorInfo(doorElement: HTMLDivElement, door: DoorType) { function setDoorInfo(doorElement: HTMLDivElement, door: DoorType) {
const labelElement: HTMLDivElement = doorElement.querySelector("[data-label]")! const labelElement: HTMLDivElement = doorElement.querySelector("[data-label]")!
const buttonElements: Array<HTMLButtonElement> = Array.from(doorElement.querySelectorAll("button")) const buttonElements: Array<HTMLButtonElement> = Array.from(doorElement.querySelectorAll("button"))
@ -108,7 +143,6 @@ const t = useTranslations(lang)
labelElement.innerHTML = door.label labelElement.innerHTML = door.label
buttonElements.forEach(button => { buttonElements.forEach(button => {
console.log(button)
button.disabled = door.state === "moving" button.disabled = door.state === "moving"
}) })
} }
@ -132,7 +166,7 @@ const t = useTranslations(lang)
const alertDivider: HTMLDivElement = document.querySelector("#alert-divider")! const alertDivider: HTMLDivElement = document.querySelector("#alert-divider")!
const badgeUsername: HTMLDivElement = document.querySelector("#badge-username")! const badgeUsername: HTMLDivElement = document.querySelector("#badge-username")!
const buttonLogin: HTMLDivElement = document.querySelector("#button-login")! const buttonLogin: HTMLDivElement = document.querySelector("#button-login")!
const usernameElement: HTMLSpanElement = badgeUsername.querySelector('#username')! const usernameElement: HTMLSpanElement = badgeUsername.querySelector("#username")!
alertUnauthenticated.classList.toggle("hidden", auth.authenticated) alertUnauthenticated.classList.toggle("hidden", auth.authenticated)
alertUnauthorized.classList.toggle("hidden", !auth.authenticated || auth.authorized) alertUnauthorized.classList.toggle("hidden", !auth.authenticated || auth.authorized)
@ -143,6 +177,7 @@ const t = useTranslations(lang)
} }
refresh() refresh()
checkUser()
setTimeout(() => { setTimeout(() => {
doors[0] = { doors[0] = {