@@ -53,24 +53,49 @@ Access the session in your endpoints or load functions via `event.locals`:
5353
5454``` ts
5555// src/routes/+page.server.ts
56- import type { Actions , PageServerLoad } from ' ./$types' ;
56+ import type { PageServerLoad } from ' ./$types' ;
5757
5858export const load: PageServerLoad = async ({ locals }) => {
59- // locals.sessionId: string
60- // locals.session: { identity: unknown | null; created: string | null }
59+ // Available on locals:
60+ // - locals.store: ISessionStore
61+ // - locals.hasher: ISessionHasher
62+ // - locals.generator: ISessionGenerator
63+ // - locals.sessionId: string (present once a session exists)
64+ // - locals.session: { identity: unknown | null; created: string | null }
6165 return {
6266 sessionId: locals .sessionId ,
6367 session: locals .session
6468 };
6569};
6670```
6771
72+ Locals interface (for reference):
73+
74+ ``` ts
75+ // src/app.d.ts
76+ import type { ISessionGenerator , ISessionHasher , ISessionStore } from ' @escendit/sveltekit-session' ;
77+
78+ declare global {
79+ namespace App {
80+ interface Locals {
81+ store: ISessionStore ;
82+ hasher: ISessionHasher ;
83+ generator: ISessionGenerator ;
84+ sessionId: string ;
85+ session: any ;
86+ }
87+ }
88+ }
89+ export {};
90+ ```
91+
6892## How it works
6993
7094- On each request the middleware checks for the configured session cookie.
7195- If a valid session exists in the store, it populates:
7296 - ` event.locals.sessionId `
7397 - ` event.locals.session = { identity: any | null, created: string | null } `
98+ - Always available: ` event.locals.store ` , ` event.locals.hasher ` , ` event.locals.generator ` (from middleware config)
7499- If there is no valid session yet:
75100 - It generates a new random ID, stores minimal fields (` identity=null ` , ` created=timestamp ` ) and sets an expiry on the store entry.
76101 - It sets a secure cookie and ` Cache-Control: no-store ` header.
0 commit comments