fix: handle backend startup delays with retry logic in auth

This commit is contained in:
2026-01-29 20:04:50 -06:00
parent ba2b2fc50a
commit 5a6ea1e53a
+31 -9
View File
@@ -24,17 +24,39 @@ class AuthStore {
return this.state.mode === "authenticated"; return this.state.mode === "authenticated";
} }
/**
* Attempt to load the current user session from the backend.
* Only transitions to "unauthenticated" on a definitive 401/403.
* Retries indefinitely on transient failures (network errors, 5xx)
* so that a slow backend startup doesn't kick the user to login.
*/
async init() { async init() {
try { const MAX_DELAY_MS = 7_000;
const response = await fetch("/api/auth/me"); let delayMs = 500;
if (response.ok) {
const user: User = await response.json(); for (;;) {
this.state = { mode: "authenticated", user }; try {
} else { const response = await fetch("/api/auth/me");
this.state = { mode: "unauthenticated" };
if (response.ok) {
const user: User = await response.json();
this.state = { mode: "authenticated", user };
return;
}
// Definitive rejection — no session or not authorized
if (response.status === 401 || response.status === 403) {
this.state = { mode: "unauthenticated" };
return;
}
// Server error (5xx) or unexpected status — retry
} catch {
// Network error (backend not up yet) — retry
} }
} catch {
this.state = { mode: "unauthenticated" }; await new Promise((r) => setTimeout(r, delayMs));
delayMs = Math.min(delayMs * 2, MAX_DELAY_MS);
} }
} }