2026-07-04 20:19:02 +02:00
|
|
|
"""Auth endpoints: login, logout, registration."""
|
2026-07-04 20:09:54 +02:00
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from auth import login, logout, require_auth
|
2026-07-04 20:19:02 +02:00
|
|
|
from fastapi import APIRouter, Depends
|
2026-07-04 20:14:34 +02:00
|
|
|
from pydantic import BaseModel, Field
|
2026-07-04 20:19:02 +02:00
|
|
|
from services.registration import register_system_admin, setup_status
|
2026-07-04 20:09:54 +02:00
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/api/auth", tags=["auth"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LoginRequest(BaseModel):
|
2026-07-04 20:14:34 +02:00
|
|
|
email: str = Field(min_length=3, max_length=255)
|
2026-07-04 20:09:54 +02:00
|
|
|
password: str = Field(min_length=8, max_length=256)
|
|
|
|
|
|
|
|
|
|
|
2026-07-04 20:19:02 +02:00
|
|
|
class RegisterRequest(BaseModel):
|
|
|
|
|
email: str = Field(min_length=3, max_length=255)
|
|
|
|
|
password: str = Field(min_length=8, max_length=256)
|
|
|
|
|
display_name: str = Field(min_length=1, max_length=255)
|
|
|
|
|
organization_name: str | None = Field(default=None, max_length=255)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/setup-status")
|
|
|
|
|
def auth_setup_status():
|
|
|
|
|
return setup_status()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/register")
|
|
|
|
|
def auth_register(body: RegisterRequest):
|
|
|
|
|
"""Erster User wird Portal-Systemadmin inkl. Default-Tenant."""
|
|
|
|
|
return register_system_admin(
|
|
|
|
|
email=body.email,
|
|
|
|
|
password=body.password,
|
|
|
|
|
display_name=body.display_name,
|
|
|
|
|
organization_name=body.organization_name,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-07-04 20:09:54 +02:00
|
|
|
@router.post("/login")
|
|
|
|
|
def auth_login(body: LoginRequest):
|
|
|
|
|
return login(body.email, body.password)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/logout")
|
2026-07-04 20:19:02 +02:00
|
|
|
def auth_logout(session: dict = Depends(require_auth)):
|
2026-07-04 20:09:54 +02:00
|
|
|
token = session.get("token")
|
|
|
|
|
if token:
|
|
|
|
|
logout(token)
|
|
|
|
|
return {"ok": True}
|