diff --git a/web/client.mjs b/web/client.mjs index 3b1e14b..0afb2e5 100644 --- a/web/client.mjs +++ b/web/client.mjs @@ -5,8 +5,8 @@ // every path leads there until a fresh one is claimed. /** @typedef {import('../server/shapes.mjs').HttpError} HttpError */ -/** One api() call: method + body + the keepalive flag. */ -/** @typedef {{ method?: string, body?: unknown, keepalive?: boolean }} ApiOpts */ +/** One api() call: method + body + the keepalive flag + the domain-401 flag. */ +/** @typedef {{ method?: string, body?: unknown, keepalive?: boolean, domain401?: boolean }} ApiOpts */ import { logEvent } from './log.mjs'; import { state } from './state.mjs'; @@ -40,7 +40,11 @@ export async function api(path, opts = {}) { e.detail = data.detail; e.status = res.status; logEvent('api', `${label} ${res.status} ${Math.round(performance.now() - started)}ms -- ${e.message}`); - if (res.status === 401 && path !== '/session' && authToken()) { + // a 401 ends the session -- except the provider probe routes, + // where 401/403 is the endpoint's own auth refusal answering the + // add or the key check (the SPA's password branch, by design); + // those call sites opt out with domain401 + if (res.status === 401 && path !== '/session' && authToken() && !opts.domain401) { clearToken(); state.me = null; logEvent('nav', 'signed out: token rejected'); diff --git a/web/view-home.mjs b/web/view-home.mjs index acd7547..1e7b4a4 100644 --- a/web/view-home.mjs +++ b/web/view-home.mjs @@ -229,7 +229,7 @@ function renderProviderCard(p, seq) { send.addEventListener('click', async () => { if (!key.value.trim()) { banner('enter the key first'); return; } try { - await api(`/providers/${encodeURIComponent(p.slug)}/auth`, { method: 'POST', body: { apiKey: key.value.trim() } }); + await api(`/providers/${encodeURIComponent(p.slug)}/auth`, { method: 'POST', body: { apiKey: key.value.trim() }, domain401: true }); } catch (e) { banner(e.message); return; } // still refused -- the ask stays open dropModelCache(); render(); @@ -379,7 +379,7 @@ function closeAddForm() { /** @type {HTMLLabelElement} */ ($('provider-key-field')).hidden = true; } $('provider-add-of').addEventListener('click', async () => { - try { await api('/providers', { method: 'POST', body: { builtin: true } }); } catch (e) { banner(e.message); return; } + try { await api('/providers', { method: 'POST', body: { builtin: true }, domain401: true }); } catch (e) { banner(e.message); return; } dropModelCache(); render(); }); @@ -393,7 +393,7 @@ $('provider-add-go').addEventListener('click', async () => { // the provider stands broken -- this go sends the key, not another add if (!apiKey) { banner('the endpoint wants a key -- enter one or cancel'); return; } try { - await api(`/providers/${encodeURIComponent(authTarget)}/auth`, { method: 'POST', body: { apiKey } }); + await api(`/providers/${encodeURIComponent(authTarget)}/auth`, { method: 'POST', body: { apiKey }, domain401: true }); } catch (e) { banner(e.message); return; } // still refused -- the ask stays open closeAddForm(); dropModelCache(); @@ -402,7 +402,7 @@ $('provider-add-go').addEventListener('click', async () => { } if (!slug || !baseUrl) { banner('slug and baseUrl are both required'); return; } try { - await api('/providers', { method: 'POST', body: { slug, baseUrl } }); + await api('/providers', { method: 'POST', body: { slug, baseUrl }, domain401: true }); } catch (e) { if (e.status === 401 || e.status === 403) { authTarget = slug;