fix: renew a session that died under an open tab, instead of eating the message typed into it
Nightly Build / build (push) Successful in 7m34s

Sessions live in the server's RAM, so a restart logs everyone out while the
browser keeps sending a cookie nobody recognises. Nothing noticed: every gated
API call answered 401 into a component that shrugged, and the chat socket was
refused at the upgrade — which reaches `onclose` looking exactly like a flaky
network, so the loop retried every 2 s forever behind "Not connected —
reconnecting, please retry", against a server that would never accept it again.

Retrying was not even the expensive part. `_send()` cleared the composer and
dropped the attachment chips *before* testing the socket, so a long message was
already destroyed by the time the error bubble appeared. The connection test now
comes first and everything below it is unreachable while the socket is down, so
the text stays where the user left it; `/new` and `/clear` move above the guard
because they go over HTTP and reconnect the socket themselves, which is when
they are most wanted.

Detection is one module (`lib/session-expiry.js`) reporting a fact — `auth-expired`,
and `auth-restored` on the way back — with nothing in it that touches the DOM. A
`window.fetch` wrapper flags any 401 from a gated `/api` path, a wrapper rather
than a helper each call site opts into because the components call `fetch`
directly in dozens of places and a seam that must be remembered is one the next
page will forget; `auth/*` and `setup/*` are excluded, where 401 is the normal
answer. The socket's own path asks `probeSession()` before retrying, since it
cannot tell a refusal from a blip. The native mobile shell is guarded inside the
report, so no future caller can reintroduce a web login form there.

The answer is a modal over the page the user is already on, not the login
screen: bouncing to it would throw away everything the page was holding —
including the half-written message this commit exists to save. One password
field, prefilled with the last username this browser logged in as, not
dismissible (with no session nothing on the page works, and a dialog you can
wave away leaves a UI that silently fails every action). On success the chat
reconnects on `auth-restored` and reconciles like any other disconnection.

Known gap: pages that failed a fetch during the outage keep their stale data
until navigated to again. Only the chat re-arms itself.
This commit is contained in:
2026-08-04 13:02:39 +01:00
parent 6cb4ea0ce8
commit ff298f1aef
10 changed files with 378 additions and 10 deletions
+38
View File
@@ -105,3 +105,41 @@
.login-spinner {
animation-name: setup-spin;
}
/* ── Re-login dialog ──────────────────────────────────────────────────────────
Raised over the page the user is already on when their session dies (see
components/session-relogin.js). Deliberately a scrim rather than an opaque
screen: seeing the work still there is the whole point of not navigating
away. Reuses `.login-card` so the two login surfaces cannot drift apart. */
.relogin-backdrop {
position: fixed;
inset: 0;
z-index: 10000; /* above the login screen's own layer */
display: flex;
align-items: center;
justify-content: center;
padding: 24px;
background: rgba(0, 0, 0, .5);
backdrop-filter: blur(2px);
overflow: auto;
}
.relogin-card {
max-width: 400px;
padding: 28px 28px 24px;
}
.relogin-title {
font-size: 1.15rem;
margin-bottom: 10px;
}
@media (prefers-reduced-motion: no-preference) {
.relogin-card { animation: relogin-in .18s ease-out; }
}
@keyframes relogin-in {
from { opacity: 0; transform: translateY(8px); }
to { opacity: 1; transform: none; }
}