Spaces:
Running
Running
Commit
Β·
59e68c5
1
Parent(s):
cceb270
Fix: Correct middleware ordering for session access in auth
Browse filesThe root cause of login button not working was middleware ordering.
In FastAPI/Starlette, middleware runs in REVERSE order (LIFO).
Before: Auth ran BEFORE Session, so request.session wasn't available
After: Session runs BEFORE Auth, properly setting up request.session
This fixes the error:
"Session access failed: SessionMiddleware must be installed to access request.session"
π€ Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- backend/app.py +13 -10
backend/app.py
CHANGED
|
@@ -56,9 +56,9 @@ app.add_middleware(
|
|
| 56 |
allow_headers=["*"],
|
| 57 |
)
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
|
| 63 |
# Detect if running in HF Spaces and adjust session config accordingly
|
| 64 |
from utils.environment import is_huggingface_space
|
|
@@ -67,32 +67,35 @@ from utils.environment import is_huggingface_space
|
|
| 67 |
logger.info(f"π Environment detection: is_huggingface_space() = {is_huggingface_space()}")
|
| 68 |
logger.info(f"π SPACE_ID env var: {os.getenv('SPACE_ID')}")
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
if is_huggingface_space():
|
| 71 |
# HF Spaces optimized session configuration
|
| 72 |
logger.info("ποΈ Configuring session middleware for HF Spaces environment")
|
| 73 |
app.add_middleware(
|
| 74 |
SessionMiddleware,
|
| 75 |
secret_key=session_secret,
|
| 76 |
-
max_age=3600, # Shorter expiry for HF Spaces (1 hour)
|
| 77 |
same_site="none", # CRITICAL: Required for iframe cookies in HF Spaces
|
| 78 |
https_only=True, # HF Spaces uses HTTPS
|
| 79 |
-
# Note: SessionMiddleware doesn't support custom cookie name, using default
|
| 80 |
)
|
| 81 |
else:
|
| 82 |
# Local development session configuration
|
| 83 |
logger.info("π Configuring session middleware for local development")
|
| 84 |
app.add_middleware(
|
| 85 |
-
SessionMiddleware,
|
| 86 |
secret_key=session_secret,
|
| 87 |
max_age=86400, # 24 hours for local dev
|
| 88 |
same_site="lax", # Better for OAuth redirects
|
| 89 |
https_only=False, # HTTP for local dev
|
| 90 |
)
|
| 91 |
|
| 92 |
-
# Add
|
| 93 |
-
app.add_middleware(ConditionalAuthMiddleware)
|
| 94 |
-
|
| 95 |
-
# Add usage tracking middleware (last, to track authenticated requests)
|
| 96 |
app.add_middleware(UsageTrackingMiddleware)
|
| 97 |
|
| 98 |
# Custom exception handler for authentication redirects
|
|
|
|
| 56 |
allow_headers=["*"],
|
| 57 |
)
|
| 58 |
|
| 59 |
+
# IMPORTANT: Middleware runs in REVERSE order of add_middleware() calls!
|
| 60 |
+
# We need Session to run BEFORE Auth so that request.session is available.
|
| 61 |
+
# Therefore: Add Auth FIRST, then Session SECOND.
|
| 62 |
|
| 63 |
# Detect if running in HF Spaces and adjust session config accordingly
|
| 64 |
from utils.environment import is_huggingface_space
|
|
|
|
| 67 |
logger.info(f"π Environment detection: is_huggingface_space() = {is_huggingface_space()}")
|
| 68 |
logger.info(f"π SPACE_ID env var: {os.getenv('SPACE_ID')}")
|
| 69 |
|
| 70 |
+
# Add auth middleware FIRST (will run AFTER session middleware)
|
| 71 |
+
app.add_middleware(ConditionalAuthMiddleware)
|
| 72 |
+
|
| 73 |
+
# Add session middleware SECOND (will run BEFORE auth middleware, setting up request.session)
|
| 74 |
+
# HF Spaces specific session configuration
|
| 75 |
+
session_secret = os.getenv("SESSION_SECRET_KEY") or secrets.token_urlsafe(32)
|
| 76 |
+
|
| 77 |
if is_huggingface_space():
|
| 78 |
# HF Spaces optimized session configuration
|
| 79 |
logger.info("ποΈ Configuring session middleware for HF Spaces environment")
|
| 80 |
app.add_middleware(
|
| 81 |
SessionMiddleware,
|
| 82 |
secret_key=session_secret,
|
| 83 |
+
max_age=3600, # Shorter expiry for HF Spaces (1 hour)
|
| 84 |
same_site="none", # CRITICAL: Required for iframe cookies in HF Spaces
|
| 85 |
https_only=True, # HF Spaces uses HTTPS
|
|
|
|
| 86 |
)
|
| 87 |
else:
|
| 88 |
# Local development session configuration
|
| 89 |
logger.info("π Configuring session middleware for local development")
|
| 90 |
app.add_middleware(
|
| 91 |
+
SessionMiddleware,
|
| 92 |
secret_key=session_secret,
|
| 93 |
max_age=86400, # 24 hours for local dev
|
| 94 |
same_site="lax", # Better for OAuth redirects
|
| 95 |
https_only=False, # HTTP for local dev
|
| 96 |
)
|
| 97 |
|
| 98 |
+
# Add usage tracking middleware (last added = runs first, outermost)
|
|
|
|
|
|
|
|
|
|
| 99 |
app.add_middleware(UsageTrackingMiddleware)
|
| 100 |
|
| 101 |
# Custom exception handler for authentication redirects
|