vectorplasticity commited on
Commit
32b5d4c
·
verified ·
1 Parent(s): fccb659

Remove AuthMiddleware, add direct session check in root endpoint only

Browse files
Files changed (1) hide show
  1. app/main.py +16 -7
app/main.py CHANGED
@@ -25,13 +25,11 @@ app.add_middleware(
25
  allow_headers=["*"],
26
  )
27
 
28
- # Setup authentication middleware and session management
29
- # Must be done BEFORE importing routers so middleware order is correct
30
- from app.auth import setup_auth, AuthMiddleware
31
  from app.config import settings
32
 
33
  setup_auth(app) # Adds session middleware
34
- app.add_middleware(AuthMiddleware) # Protects routes based on auth config
35
 
36
  # Import routers AFTER app is created and middleware is configured
37
  from app.routers import models, datasets, training, system, jobs
@@ -59,10 +57,21 @@ async def login_page():
59
  from app.auth import get_login_html
60
  return HTMLResponse(content=get_login_html())
61
 
62
- # Root endpoint - serves dashboard
63
  @app.get("/", response_class=HTMLResponse)
64
- async def root():
65
- """Serve the main dashboard"""
 
 
 
 
 
 
 
 
 
 
 
66
  template_path = os.path.join(os.path.dirname(__file__), "templates", "dashboard.html")
67
  try:
68
  with open(template_path, 'r') as f:
 
25
  allow_headers=["*"],
26
  )
27
 
28
+ # Setup authentication - session middleware only (no custom middleware)
29
+ from app.auth import setup_auth
 
30
  from app.config import settings
31
 
32
  setup_auth(app) # Adds session middleware
 
33
 
34
  # Import routers AFTER app is created and middleware is configured
35
  from app.routers import models, datasets, training, system, jobs
 
57
  from app.auth import get_login_html
58
  return HTMLResponse(content=get_login_html())
59
 
60
+ # Root endpoint - serves dashboard (with auth check)
61
  @app.get("/", response_class=HTMLResponse)
62
+ async def root(request: Request):
63
+ """Serve the main dashboard - check auth first if enabled"""
64
+ # Check if auth is required and user is logged in
65
+ if settings.APP_PASSWORD:
66
+ from app.auth import is_authenticated
67
+ if not is_authenticated(request):
68
+ # Redirect to login page
69
+ return HTMLResponse(
70
+ content='<script>window.location.href="/login";</script>',
71
+ status_code=302,
72
+ headers={"location": "/login"}
73
+ )
74
+
75
  template_path = os.path.join(os.path.dirname(__file__), "templates", "dashboard.html")
76
  try:
77
  with open(template_path, 'r') as f: