Lightweight session middleware for SvelteKit. It issues and persists a secure session ID, stores minimal session data in a pluggable store, and exposes the session on event.locals.
Works out of the box with an in‑memory store for development, and includes a Redis store for production when running on Bun.
- Simple
SessionMiddlewareyou add tohooks.server.ts - Pluggable session storage via
ISessionStore(in‑memory and Redis implementations included) - Secure cookie with
httpOnly,secure,sameSite: 'strict',priority: 'high' - Session ID generation using Node crypto and base58 encoding by default
- Sensible defaults with runtime validation
- Explicit behavior for first request and mutation safety
npm i @escendit/sveltekit-session
# or
pnpm add @escendit/sveltekit-session
# or
bun add @escendit/sveltekit-sessionPeer dependencies:
svelte@^5@sveltejs/kit@^2(app peer, used by you)
Notes:
- The default in‑memory store works everywhere (Node/Bun) and is great for local development.
- The provided
RedisSessionStoreuses Bun's built‑in Redis client. If you want Redis, run your app with Bun.
Add the middleware to your src/hooks.server.ts:
// src/hooks.server.ts
import { sequence } from '@sveltejs/kit/hooks';
import type { Handle } from '@sveltejs/kit';
import { SessionMiddleware } from '@escendit/sveltekit-session';
export const handle: Handle = sequence(
SessionMiddleware({
cookie: 'session.id',
expireIn: 60 * 60 * 24 // 1 day (seconds)
})
);Access the session in your endpoints or load functions via event.locals:
// src/routes/+page.server.ts
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ locals }) => {
// Available on locals:
// - locals.store: ISessionStore
// - locals.hasher: ISessionHasher
// - locals.generator: ISessionGenerator
// - locals.sessionId: string (present once a session exists)
// - locals.session: { identity: unknown | null; created: string | null }
return {
sessionId: locals.sessionId,
session: locals.session
};
};Locals interface (for reference):
// src/app.d.ts
import type { ISessionGenerator, ISessionHasher, ISessionStore } from '@escendit/sveltekit-session';
declare global {
namespace App {
interface Locals {
store: ISessionStore;
hasher: ISessionHasher;
generator: ISessionGenerator;
sessionId: string;
session: any;
}
}
}
export {};- On each request the middleware checks for the configured session cookie.
- If a valid session exists in the store, it populates:
event.locals.sessionIdevent.locals.session = { identity: any | null, created: string | null }- Always available:
event.locals.store,event.locals.hasher,event.locals.generator(from middleware config)
- If there is no valid session yet:
- It generates a new random ID, stores minimal fields (
identity=null,created=timestamp) and sets an expiry on the store entry. - It sets a secure cookie and
Cache-Control: no-storeheader. - For
GETrequests, it performs a303redirect to the same URL so the browser will include the cookie on the next request. - For non‑
GETrequests without an existing session, it returns405to ensure the client establishes a session viaGETfirst.
- It generates a new random ID, stores minimal fields (
All options are optional; the following are the defaults applied internally:
cookie:"session.id"expireIn:86400(seconds)size:128(entropy bytes for ID generation)sessionStore:new InMemorySessionStore()sessionHasher:new DefaultSessionHasher()(encodes to base58)sessionGenerator:new DefaultSessionGenerator()(usescrypto.randomBytes)
Example with custom options:
import { SessionMiddleware, InMemorySessionStore } from '@escendit/sveltekit-session';
export const handle = sequence(
SessionMiddleware({
cookie: 'sid',
expireIn: 60 * 10, // 10 minutes
size: 256,
sessionStore: new InMemorySessionStore()
})
);This library keeps the session store minimal by design. If you want to associate an identity (e.g., after login), create and reuse the same store instance you pass into the middleware.
// src/lib/session-store.ts
import { InMemorySessionStore } from '@escendit/sveltekit-session';
export const sessionStore = new InMemorySessionStore();// src/hooks.server.ts
import { sequence } from '@sveltejs/kit/hooks';
import { SessionMiddleware } from '@escendit/sveltekit-session';
import { sessionStore } from '$lib/session-store';
export const handle = sequence(
SessionMiddleware({ sessionStore })
);// src/routes/login/+page.server.ts (example)
import type { Actions } from './$types';
import { sessionStore } from '$lib/session-store';
export const actions: Actions = {
default: async ({ locals }) => {
const sessionKey = `session:${locals.sessionId}`;
await sessionStore.setMultiple(sessionKey, [
'identity', JSON.stringify({ userId: '123', roles: ['user'] })
]);
// Optionally refresh TTL
// await sessionStore.expire(sessionKey, 60 * 60 * 24);
return { success: true };
}
};The middleware will deserialize the identity JSON on subsequent requests and expose it on locals.session.identity.
The package includes a Redis store that uses Bun's built‑in Redis client.
// src/lib/session-store.ts
import { RedisSessionStore } from '@escendit/sveltekit-session';
export const sessionStore = new RedisSessionStore();// src/hooks.server.ts
import { sequence } from '@sveltejs/kit/hooks';
import { SessionMiddleware } from '@escendit/sveltekit-session';
import { sessionStore } from '$lib/session-store';
export const handle = sequence(
SessionMiddleware({ sessionStore, cookie: 'sid', expireIn: 60 * 60 })
);Requirements:
- Run your SvelteKit server with Bun (
bun run dev/bun run start). - Ensure your Redis instance is reachable per Bun's configuration. See Bun docs for
bun:redis.
Package exports:
SessionMiddleware(sessionConfig?: SessionConfig): Handletype SessionConfig— public configuration shapetype ISessionStore— storage interface withexists,expire,getSingle,setSingle,getMultiple,setMultipletype ISessionHasher—hash(buffer: Uint8Array): stringtype ISessionGenerator—generate(size: number): Uint8ArrayInMemorySessionStore— default dev storeRedisSessionStore— Bun Redis storeDefaultSessionGenerator— uses Nodecrypto.randomBytesDefaultSessionHasher— base58 encoding
Common scripts from package.json:
dev— start vite dev server (Bun)build— build library and packagepreview— preview built appcheck/check:watch— typecheck withsvelte-checkformat/lint— Prettiertest— run unit tests (Vitest)
- Cookies are set with
secure: true; when testing locally onhttp://localhost, ensure your browser still handles the cookie as expected or use HTTPS. - First request sets the session; non‑GET requests before a session is established will receive
405. - The library sets
Cache-Control: no-storeon session‑issuing responses to avoid caching issues.
Licensed under the Apache License, Version 2.0. See the LICENSE file for the full text.
Copyright (c) 2025 Escendit.