diff options
Diffstat (limited to 'web/src/lib/protocol/session.ts')
| -rw-r--r-- | web/src/lib/protocol/session.ts | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/web/src/lib/protocol/session.ts b/web/src/lib/protocol/session.ts new file mode 100644 index 0000000..1025288 --- /dev/null +++ b/web/src/lib/protocol/session.ts @@ -0,0 +1,25 @@ +/** + * Establish the persistent session cookie (PLAN.md section 3.3) before the + * game WebSocket connects. + * + * The `/session` endpoint mints a session id on the first call and sets the + * `x-haven-session` cookie (HttpOnly). The browser then carries it on every + * same-origin request — including the `/ws` upgrade — so a player keeps the + * same session id across page reloads *and* server restarts. That is what + * lets the server's restored seats (persisted and keyed by session id) land + * back on the right player after a crash; without the cookie the upgrade + * would mint a fresh session and the restored seat would be orphaned. + * + * The id also arrives in the `welcome` message, but that is too late to seed + * the cookie the upgrade depends on — hence this explicit bootstrap call. + */ +export async function establishSessionCookie(): Promise<void> { + try { + // `same-origin` credentials send/receive the cookie; the response + // sets it if the browser has none yet, and is a no-op set otherwise. + await fetch('/session', { credentials: 'same-origin' }); + } catch { + // Offline or blocked: the WS upgrade will still (re)mint a session; + // the tab just starts as a fresh session instead of a restored one. + } +} |
