Resume Heimdall login across Discord navigation

This commit is contained in:
Meta Vens 2026-08-20 20:57:05 +02:00
commit 7b58e3eaa2
3 changed files with 18 additions and 9 deletions

File diff suppressed because one or more lines are too long

2
web/dist/index.html vendored
View file

@ -1,5 +1,5 @@
<!doctype html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>GhostlightDungeon</title> <script type="module" crossorigin src="/assets/index-BdgXKt11.js"></script>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>GhostlightDungeon</title> <script type="module" crossorigin src="/assets/index-DcMdsMeE.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-B7Qh0xAi.css">
</head>
<body><header><p class="eyebrow">STARFIRE / PRIVATE VERSE</p><h1>GhostlightDungeon</h1><p id="status" role="status" aria-live="polite">Reading the world…</p></header><main>

View file

@ -160,9 +160,8 @@ async function startHeimdallLogin() {
if (!response.ok || typeof body?.authorization_url !== "string" || typeof body?.attempt_id !== "string") {
throw new ServerResponseFailure(responseError(body, "Heimdall could not start Discord sign-in."));
}
const popup = window.open(body.authorization_url, "ghostlight-heimdall", "popup,width=560,height=760");
if (!popup) throw new Error("Allow pop-ups for Ghostlight so Heimdall can complete Discord sign-in.");
await pollHeimdallAttempt(body.attempt_id);
sessionStorage.setItem("ghostlight_heimdall_attempt", body.attempt_id);
window.location.assign(body.authorization_url);
}
async function pollHeimdallAttempt(attemptId: string) {
@ -173,16 +172,26 @@ async function pollHeimdallAttempt(attemptId: string) {
const body = await decodeResponse(response);
if (!response.ok) throw new ServerResponseFailure(responseError(body, "Heimdall sign-in expired."));
if (body?.status === "pending") continue;
if (body?.status !== "succeeded") throw new Error(String(body?.error ?? "Your Discord account does not have Ghostlight playtest access."));
if (body?.status !== "succeeded") {
sessionStorage.removeItem("ghostlight_heimdall_attempt");
throw new Error(String(body?.error ?? "Your Discord account does not have Ghostlight playtest access."));
}
const adopted = await fetch(`/api/auth/heimdall/attempt/${encodeURIComponent(attemptId)}/adopt`, { method: "POST" });
const adoptedBody = await decodeResponse(adopted);
if (!adopted.ok) throw new ServerResponseFailure(responseError(adoptedBody, "Ghostlight could not adopt the Heimdall session."));
sessionStorage.removeItem("ghostlight_heimdall_attempt");
await refresh();
return;
}
sessionStorage.removeItem("ghostlight_heimdall_attempt");
throw new Error("Heimdall sign-in timed out. Try again when you are ready.");
}
async function resumeHeimdallLogin() {
const attemptId = sessionStorage.getItem("ghostlight_heimdall_attempt");
if (attemptId) await pollHeimdallAttempt(attemptId);
}
async function refresh() {
const response = await fetch("/api/surface");
if (response.status === 401) { showAuthenticationGate(); return; }
@ -489,4 +498,4 @@ document.querySelector<HTMLButtonElement>("#load-operator")!.addEventListener("c
renderEveSurface(surface, document.querySelector<HTMLElement>("#operator-output")!, { body: document.body, clientId: "ghostlight.operator", statusElement: status });
});
});
void refresh().catch(reportClientFailure);
void refresh().then(resumeHeimdallLogin).catch(reportClientFailure);