check if user is logged in
This commit is contained in:
parent
72a253b498
commit
2a65cd9f06
1 changed files with 39 additions and 4 deletions
|
|
@ -48,7 +48,10 @@ const t = useTranslations(lang)
|
|||
<DoorTemplate state="unknown"/>
|
||||
|
||||
<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 template: HTMLTemplateElement = document.querySelector("#template-door")!
|
||||
|
|
@ -63,12 +66,13 @@ const t = useTranslations(lang)
|
|||
username: string;
|
||||
authorized: boolean;
|
||||
authenticated: boolean;
|
||||
until: Date | null;
|
||||
}
|
||||
|
||||
const auth: AuthType = {
|
||||
username: "user",
|
||||
authorized: true,
|
||||
authenticated: false,
|
||||
until: null,
|
||||
}
|
||||
|
||||
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) {
|
||||
const labelElement: HTMLDivElement = doorElement.querySelector("[data-label]")!
|
||||
const buttonElements: Array<HTMLButtonElement> = Array.from(doorElement.querySelectorAll("button"))
|
||||
|
|
@ -108,7 +143,6 @@ const t = useTranslations(lang)
|
|||
labelElement.innerHTML = door.label
|
||||
|
||||
buttonElements.forEach(button => {
|
||||
console.log(button)
|
||||
button.disabled = door.state === "moving"
|
||||
})
|
||||
}
|
||||
|
|
@ -132,7 +166,7 @@ const t = useTranslations(lang)
|
|||
const alertDivider: HTMLDivElement = document.querySelector("#alert-divider")!
|
||||
const badgeUsername: HTMLDivElement = document.querySelector("#badge-username")!
|
||||
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)
|
||||
alertUnauthorized.classList.toggle("hidden", !auth.authenticated || auth.authorized)
|
||||
|
|
@ -143,6 +177,7 @@ const t = useTranslations(lang)
|
|||
}
|
||||
|
||||
refresh()
|
||||
checkUser()
|
||||
|
||||
setTimeout(() => {
|
||||
doors[0] = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue