Files
stand/auth/login.py
T

58 lines
1.6 KiB
Python

import logging
from fastapi.responses import RedirectResponse
from nicegui import APIRouter, app, ui
from state import config
log = logging.getLogger(__name__)
router = APIRouter()
if config.fake:
log.warning("using fake LDAP")
from auth.fakeldap import get_data
else:
from auth.psildap import get_data
@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:
log.exception(f'login failed for user "{username.value}"')
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