Files
snek/src/snek/form/login.py
T
retoor 8bacd6aa3f feat: add debug logging option to serve command
refactor: reorganize imports and improve error handling in application
fix: correct typo in ip2location middleware
chore: add author headers to source files
2025-12-18 23:48:50 +01:00

54 lines
1.3 KiB
Python

# retoor <retoor@molodetz.nl>
from snek.system.form import Form, FormButtonElement, FormInputElement, HTMLElement
class AuthField(FormInputElement):
@property
async def errors(self):
result = await super().errors
if self.model.password.value and self.model.username.value:
if not await self.app.services.user.validate_login(
self.model.username.value, self.model.password.value
):
return ["Invalid username or password"]
return result
class LoginForm(Form):
title = HTMLElement(tag="h1", text="Login")
username = AuthField(
name="username",
required=True,
min_length=2,
max_length=20,
regex=r"^[a-zA-Z0-9_-]+$",
place_holder="Username",
type="text",
)
password = AuthField(
name="password",
required=True,
min_length=1,
type="password",
place_holder="Password",
)
action = FormButtonElement(
name="action", value="submit", text="Login", type="button"
)
@property
async def is_valid(self):
return all(
[
self["username"],
self["password"],
not await self.username.errors,
not await self.password.errors,
]
)