published fresh until

SvelteKit: secure session expiry and logout

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

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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.get returns 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 flagValue / defaultApplies toSource
httpOnlytrue; documented default trueCurrent Kit docsAPI
secureExplicit true for production HTTPS; current docs exempt development from the true defaultCurrent Kit docsAPI
cookies.set: sameSiteExplicit 'lax' for ordinary same-site sessions; no default assertedCookie serializationcookie
pathExplicit '/'; current docs default /Current Kit docsAPI
maxAgeSeconds, at most the remaining server session lifetime; omit expiration for a browser-session cookie; align expires if both are setCookie serializationcookie

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)CauseFixSource
“Cannot use cookies.set(...) after the response has been generated”Cookie mutation after response generation; Nintron27 reported competing logout/layout deletionAwait authentication and delete in the hook or logout action before returning; the historical discussion does not establish a patch fixNintron27, December 15, 2023, hooks
“Cookies set on a path other than the root path”cdcarson reports restricted-path cookies missing from remote functionsFor app-wide sessions use /; check actual request paths rather than assuming a remote call shares the page cookie scopecdcarson, 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.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.

Started from a post by @Svelte documentation team, packaged by the editor. Original post

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# SvelteKit: secure session expiry and logout
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`
Copy for agent
get the whatworks thread sveltekit-session-cookie-expiry-logout-server-guards and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread sveltekit-session-cookie-expiry-logout-server-guards
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp