57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
from fastapi.responses import RedirectResponse
|
|
from nicegui import APIRouter, app, ui
|
|
|
|
|
|
#TODO
|
|
passwords = {"a": "a", "b": "b", "c": "c"}
|
|
pgroups = {"a": {"p11111", "p22222"}, "b": {"p33333", "p44444"}}
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.page("/login")
|
|
def login(redirect_to: str = "/") -> RedirectResponse | None:
|
|
if app.storage.user.get("authenticated", False):
|
|
return RedirectResponse("/")
|
|
|
|
def try_login(): # local function to avoid passing username and password as arguments
|
|
if not username.value:
|
|
ui.notify("Missing username", color="negative")
|
|
focus(username)
|
|
return
|
|
|
|
if not password.value:
|
|
focus(password)
|
|
return
|
|
|
|
if passwords.get(username.value) != password.value:
|
|
ui.notify("Wrong username or password", color="negative")
|
|
return
|
|
|
|
app.storage.user.update(
|
|
username=username.value,
|
|
authenticated=True,
|
|
pgroups=list(pgroups.get(username.value, set()))
|
|
)
|
|
|
|
ui.navigate.to(redirect_to) # go back to where the user wanted to go
|
|
|
|
with ui.card().classes("absolute-center items-stretch"):
|
|
ui.image("icon.png")
|
|
username = ui.input("Username").props("autofocus")
|
|
password = ui.input("Password", password=True, password_toggle_button=True)
|
|
username.on("keydown.enter", try_login)
|
|
password.on("keydown.enter", try_login)
|
|
ui.button("log in", icon="login", on_click=try_login)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def focus(element):
|
|
ui.run_javascript(f"getElement({element.id}).$refs.qRef.focus()")
|
|
|
|
|
|
|