How to use createWorkersKVSessionStorage correctly? #9492
-
Following the documentation: const { getSession, commitSession, destroySession } =
createWorkersKVSessionStorage({
// The KV Namespace where you want to store sessions
kv: YOUR_NAMESPACE,
cookie: sessionCookie,
}); I'm not sure where I ended up putting it in export const getLoadContext: GetLoadContextFunction<Env> = ({ context, request }) => {
const { getSession, commitSession, destroySession } = createWorkersKVSessionStorage({
kv: context.cloudflare.env.KV,
cookie: createCookie('__session', {
httpOnly: true,
path: '/',
sameSite: true,
secrets: ['r3m1xr0ck5'],
secure: isProduction(),
}),
});
return {
...context,
sessionStorage: {
get: () => getSession(request.headers.get('Cookie')),
commit: commitSession,
destroy: destroySession,
},
} satisfies AppLoadContext;
}; This approach works for However
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Here's my next attempt: // app/sessions.server.ts
import { getPlatformProxy } from 'wrangler';
export const { getSession, commitSession, destroySession } = createWorkersKVSessionStorage<
SessionData,
SessionFlashData
>({
kv: (await getPlatformProxy<Env>()).env.YOUR_NAMESPACE,
cookie: createCookie('__session', {
httpOnly: true,
path: '/',
sameSite: true,
secrets: ['r3m1xr0ck5'],
secure: isProduction(),
}),
}); However, esbuild doesn't like it at all:
|
Beta Was this translation helpful? Give feedback.
-
Ok, so I ended up copying the code directly from the |
Beta Was this translation helpful? Give feedback.
Ok, so I ended up copying the code directly from the
createWorkersKVSessionStorage
function source. This seems to be exactly what I want, and gives me more control over it anyway.