49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
from fastapi.responses import RedirectResponse
|
|
from nicegui import APIRouter, app, ui
|
|
|
|
from auth.fakeldap import get_data
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.page("/login")
|
|
def login(redirect_to: str = "/") -> RedirectResponse | None:
|
|
if app.storage.user.get("authenticated"):
|
|
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")
|
|
username.run_method("focus")
|
|
return
|
|
|
|
if not password.value:
|
|
password.run_method("focus")
|
|
return
|
|
|
|
try:
|
|
data = get_data(username.value, password.value)
|
|
except Exception as e:
|
|
en = type(e).__name__
|
|
print(f"{en}: {e}") #TODO replace with logging
|
|
ui.notify("Wrong username or password", color="negative")
|
|
return
|
|
|
|
app.storage.user.update(data, authenticated=True)
|
|
|
|
ui.navigate.to(redirect_to) # go back to where the user wanted to go
|
|
|
|
with ui.card().classes("absolute-center items-stretch"):
|
|
ui.image("assets/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
|
|
|
|
|
|
|