Show 'Link Discord' only when Discord linking is configured (#213)

The account menu offered 'Link Discord' to every signed-in user even on a
server with no Discord OAuth app configured, where it dead-ends (the start route
303-bounces to /alerts). Add GET /api/v2/discord/config reporting whether linking
is enabled, and have the menu render the entry only when it is. On a server
without Discord set up nothing surfaces; setting the OAuth env vars later makes
the entry appear on its own — no code change needed to turn it on.
This commit is contained in:
Emi Griffith 2026-07-19 21:36:44 -07:00 committed by GitHub
parent 6e41d0b692
commit bbabf9e8f9

View file

@ -6,6 +6,8 @@
// every page the way units.js is (imported by each page's entry module).
let currentUser = null; // {id, email, display_name} or null
let discordEnabled = false; // is Discord linking configured on the server?
let discordChecked = false; // have we asked yet? (once per page load)
const authCbs = []; // notified on login/logout so pages can re-gate
// The app's base path (e.g. "/thermograph"), derived from this module's own URL
@ -59,6 +61,16 @@ async function refreshUser() {
const res = await apiFetch("api/v2/users/me");
currentUser = res.ok ? await res.json() : null;
} catch (e) { currentUser = null; }
// Learn once whether Discord linking is configured, so the account menu only
// offers "Link Discord" when it will actually work (checked lazily, and only
// for a signed-in user — the menu never shows it to anyone else).
if (currentUser && !discordChecked) {
discordChecked = true;
try {
const r = await apiFetch("api/v2/discord/config");
if (r.ok) discordEnabled = (await r.json()).enabled === true;
} catch (e) { /* leave it hidden */ }
}
return currentUser;
}
@ -304,7 +316,7 @@ function renderHeader() {
${currentUser.discord_id
? `<button type="button" class="acct-pop-link acct-discord-dm">${currentUser.discord_dm ? "Discord alerts: on" : "Discord alerts: off"}</button>`
+ '<button type="button" class="acct-pop-link acct-discord-unlink">Unlink Discord</button>'
: `<a href="${APP_BASE}/api/v2/discord/link/start" class="acct-pop-link">Link Discord</a>`}
: (discordEnabled ? `<a href="${APP_BASE}/api/v2/discord/link/start" class="acct-pop-link">Link Discord</a>` : "")}
<button type="button" class="acct-pop-link acct-signout">Sign out</button>
</div>
</div>`;