What this thread does
Do first: set the session cookie with httpOnly: true, secure: true, sameSite: 'lax' and path: '/', then validate its server-side session before resolve(event).
Short answer
As of November 2026 (checked 2026-11-24), SvelteKit 3.4.0 uses the current cookie and hook APIs below. Enforce server-side expiry and revocation on every protected request, including direct endpoints.
Choose / don't choose
- Session ID: choose when immediate server revocation matters; don't authorize merely because
cookies.getreturns a string (auth). - Hook guard: choose to cover multiple protected routes; don't rely on layout load alone. Keep object-level permissions in the endpoint/action too (auth).
- App-wide path: choose
/for shared server routes; don't use a narrow path as an authorization boundary.
Not for: choosing a replacement for Lucia; see sveltekit-auth-after-lucia-better-auth-or-owned-sessions.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
httpOnly | true; documented default true | Current Kit docs | API |
secure | Explicit true for production HTTPS; current docs exempt development from the true default | Current Kit docs | API |
cookies.set: sameSite | Explicit 'lax' for ordinary same-site sessions; no default asserted | Cookie serialization | cookie |
path | Explicit '/'; current docs default / | Current Kit docs | API |
maxAge | Seconds, at most the remaining server session lifetime; omit expiration for a browser-session cookie; align expires if both are set | Cookie serialization | cookie |
Order of operations
1. In handle, initialize event.locals.user and session state as unauthenticated. Read cookies.get('session'); look up the session, check expiry/revocation and user status before assigning authenticated locals. The hook receives a resolve function that renders the route (hooks, auth). 2. For an invalid or expired ID, delete the cookie before response generation and retain null locals. Reject protected API requests with an unauthorized response; redirect protected pages to login. Allow explicitly public routes. This rejection policy is application code, not an automatic cookie API feature. 3. Call resolve(event) only after authentication state is ready. Each sensitive endpoint/action checks its required permissions from locals. Prefer checks in +page.server.js over +layout.server.js (auth). 4. On a protected logout mutation, revoke the server session, delete the cookie and clear locals before returning/redirecting. A browser deletion alone cannot revoke a copied token. Use the application's CSRF protection on mutations.
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
“Cannot use cookies.set(...) after the response has been generated” | Cookie mutation after response generation; Nintron27 reported competing logout/layout deletion | Await authentication and delete in the hook or logout action before returning; the historical discussion does not establish a patch fix | Nintron27, December 15, 2023, hooks |
| “Cookies set on a path other than the root path” | cdcarson reports restricted-path cookies missing from remote functions | For app-wide sessions use /; check actual request paths rather than assuming a remote call shares the page cookie scope | cdcarson, November 9, 2025 |
Missing
- The linked source does not confirm: A cookie is transport, not proof of authentication.
- The linked source does not confirm: the requirement for
cookies.deleteto repeat the originalpathanddomain, including the{ path: '/' }deletion recipe. - The required expiry duration and session store are unspecified;
maxAgecannot supply server-side expiry or a universal lifetime. - Cross-site embedded authentication needs a separate cookie and CSRF design; this card selects same-site sessions.
Started from a post by @Svelte documentation team, packaged by the editor. Original post