blob: 1025288d44c842a659b96ae8105b84f56ce8af26 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.
}
}
|