fix(middleware): prevent premature redirect to /login during loading

Users were incorrectly redirected to /login during the initial loading phase
before the loading was actually complete. Added loading_started and
loading_complete flags to properly track the initialization state so
the setup redirect middleware knows when it's safe to redirect.
This commit is contained in:
2026-06-07 20:23:11 +02:00
parent b800158648
commit de250bdd37
5 changed files with 43 additions and 7 deletions

View File

@@ -118,6 +118,20 @@ class SetupRedirectMiddleware(BaseHTTPMiddleware):
return bool(other.get('unresolved_completed', False))
except Exception:
return False
def _is_loading_complete(self) -> bool:
"""Check if initial loading has completed.
Returns:
True if loading is complete, False otherwise
"""
try:
config_service = get_config_service()
config = config_service.load_config()
other = config.other or {}
return bool(other.get('loading_complete', False))
except Exception:
return False
async def dispatch(
self, request: Request, call_next: Callable
@@ -149,14 +163,17 @@ class SetupRedirectMiddleware(BaseHTTPMiddleware):
# Handle phase query parameter
phase = query_params.get("phase")
if phase == "initial":
# phase=initial should not be accessed after setup is complete
# Redirect to login
return RedirectResponse(url="/login", status_code=302)
# Only redirect if loading has actually completed
# If loading_started=True but loading_complete=False, user should stay
# on loading page to see progress
if self._is_loading_complete():
return RedirectResponse(url="/login", status_code=302)
# Otherwise, allow access to loading page (loading in progress)
elif not phase:
# No phase specified and setup is complete
# Redirect to login since user should be further in the flow
return RedirectResponse(url="/login", status_code=302)
# phase=nfo is allowed - it triggers the NFO scan phase
# No phase specified and loading is complete
if self._is_loading_complete():
return RedirectResponse(url="/login", status_code=302)
# phase=nfo is always allowed - it triggers the NFO scan phase
# Skip setup check for exempt paths
if self._is_path_exempt(path):