- msal_shell.html: show "Sign in with Microsoft" button on fresh load instead of auto-redirecting - upload.html, preview.html: prefix HTMX POST paths with root_path to fix 404s under /Pimco-charts - middleware.py: exempt /static/ paths from auth check so CSS/fonts load unauthenticated Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
27 lines
967 B
Python
27 lines
967 B
Python
from starlette.middleware.base import BaseHTTPMiddleware
|
|
from starlette.requests import Request
|
|
from starlette.responses import RedirectResponse, Response
|
|
|
|
EXEMPT_PATHS = {"/", "", "/auth/token", "/auth/logout"}
|
|
|
|
|
|
class AuthMiddleware(BaseHTTPMiddleware):
|
|
async def dispatch(self, request: Request, call_next):
|
|
path = request.url.path
|
|
root_path = request.scope.get("root_path", "")
|
|
if root_path and path.startswith(root_path):
|
|
path = path[len(root_path):]
|
|
|
|
if path in EXEMPT_PATHS or path.startswith("/static/"):
|
|
return await call_next(request)
|
|
|
|
if not request.session.get("user"):
|
|
root = root_path
|
|
if request.headers.get("HX-Request"):
|
|
return Response(
|
|
status_code=401,
|
|
headers={"HX-Redirect": f"{root}/"},
|
|
)
|
|
return RedirectResponse(url=f"{root}/")
|
|
|
|
return await call_next(request)
|