Download app/routers/auth.py from vectorplasticity/universal-model-trainer: direct link, hf CLI and curl.
- Browser
- Download file 1.37 kB
-
https://huggingface.co/spaces/vectorplasticity/universal-model-trainer/resolve/main/app/routers/auth.py
- Command line
-
hf download hf://spaces/vectorplasticity/universal-model-trainer/app/routers/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/vectorplasticity/universal-model-trainer/resolve/main/app/routers/auth.py
1.37 kB
| """ | |
| Authentication Router for Universal Model Trainer | |
| Handles login, logout, and auth status endpoints | |
| """ | |
| from fastapi import APIRouter, Request, HTTPException, Form | |
| from fastapi.responses import JSONResponse | |
| from app.auth import verify_password | |
| from app.config import settings | |
| router = APIRouter(prefix="/api/auth", tags=["Authentication"]) | |
| async def login(request: Request, password: str = Form(...)): | |
| """Login with password""" | |
| if not verify_password(password): | |
| raise HTTPException(status_code=401, detail="Invalid password") | |
| request.session["user"] = { | |
| "authenticated": True, | |
| "username": "admin" | |
| } | |
| return {"success": True, "message": "Logged in successfully"} | |
| async def logout(request: Request): | |
| """Logout and clear session""" | |
| request.session.clear() | |
| return {"success": True, "message": "Logged out successfully"} | |
| async def check_auth(request: Request): | |
| """Check if user is authenticated""" | |
| user = request.session.get("user") | |
| if user and user.get("authenticated"): | |
| return {"authenticated": True} | |
| # If no password is configured, consider authenticated | |
| if not settings.APP_PASSWORD: | |
| return {"authenticated": True, "no_password_required": True} | |
| return {"authenticated": False} | |