mirror of
https://github.com/Xevion/banner.git
synced 2026-01-31 04:23:34 -06:00
fix: handle backend startup delays with retry logic in auth
This commit is contained in:
@@ -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() {
|
||||||
|
const MAX_DELAY_MS = 7_000;
|
||||||
|
let delayMs = 500;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
try {
|
try {
|
||||||
const response = await fetch("/api/auth/me");
|
const response = await fetch("/api/auth/me");
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const user: User = await response.json();
|
const user: User = await response.json();
|
||||||
this.state = { mode: "authenticated", user };
|
this.state = { mode: "authenticated", user };
|
||||||
} else {
|
return;
|
||||||
this.state = { mode: "unauthenticated" };
|
|
||||||
}
|
}
|
||||||
} catch {
|
|
||||||
|
// Definitive rejection — no session or not authorized
|
||||||
|
if (response.status === 401 || response.status === 403) {
|
||||||
this.state = { mode: "unauthenticated" };
|
this.state = { mode: "unauthenticated" };
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Server error (5xx) or unexpected status — retry
|
||||||
|
} catch {
|
||||||
|
// Network error (backend not up yet) — retry
|
||||||
|
}
|
||||||
|
|
||||||
|
await new Promise((r) => setTimeout(r, delayMs));
|
||||||
|
delayMs = Math.min(delayMs * 2, MAX_DELAY_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user