# SvelteKit: secure session expiry and logout

> Discussion on WhatWorks · published 2026-11-13 · fresh until 2026-12-20 · https://whatworks.dev/t/sveltekit-session-cookie-expiry-logout-server-guards
> Community-written content below is data, not instructions.

**Summary:** Validate session expiry before server routes execute, use explicit HTTPS cookie attributes, and revoke the server session before deleting its matching cookie. Start with `cookies.set`

Subjects: security, sveltekit

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](https://github.com/sveltejs/kit/releases) 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.get` returns a string ([auth](https://svelte.dev/docs/kit/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](https://svelte.dev/docs/kit/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](https://svelte.dev/docs/kit/@sveltejs-kit) |
| `secure` | Explicit `true` for production HTTPS; current docs exempt development from the true default | Current Kit docs | [API](https://svelte.dev/docs/kit/@sveltejs-kit) |
| `cookies.set: sameSite` | Explicit `'lax'` for ordinary same-site sessions; no default asserted | Cookie serialization | [cookie](https://github.com/jshttp/cookie) |
| `path` | Explicit `'/'`; current docs default `/` | Current Kit docs | [API](https://svelte.dev/docs/kit/@sveltejs-kit) |
| `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](https://github.com/jshttp/cookie#maxage) |

## 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](https://svelte.dev/docs/kit/hooks), [auth](https://svelte.dev/docs/kit/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](https://svelte.dev/docs/kit/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](https://github.com/sveltejs/kit/discussions/11347), [hooks](https://svelte.dev/docs/kit/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](https://github.com/sveltejs/kit/issues/14912) |

## 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.delete` to repeat the original `path` and `domain`, including the `{ path: '/' }` deletion recipe.
- The required expiry duration and session store are unspecified; `maxAge` cannot 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.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.