Registry / web-framework / astro-cookie-session

astro-cookie-session

JSON →
library1.3.0jsnpmunverified

Middleware for managing session data using cookies in Astro SSR applications. Current version 1.3.0 (released 2024-12, active development). Session data is encrypted and stored client-side in cookies, not on the server, making it suitable for stateless architectures. Supports TypeScript with type-safe session definitions and optional flash messages. Key differentiators: zero external dependencies beyond Astro, encryption of payloads (added in 1.3.0), simple API similar to remix-run session handling, and built-in flash message support.

npm install astro-cookie-session
INSTALL
IMPORT
SIG · ASTRO-COOKIE-SESSI
A
astro-cookie-session
web-frameworkjavascriptv1.3.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createCookieSessionStorage
import { createCookieSessionStorage } from 'astro-cookie-session'
import createCookieSessionStorage from 'astro-cookie-session'
Named export only; default import will not work.
getSession
const { getSession } = createCookieSessionStorage<MyData>()
const session = getSession(Astro.cookies) // correct usage
getSession is returned from createCookieSessionStorage, not directly exported from package.
SessionData type (implicit)
type SessionData = { userId: string }
Define your own SessionData type and pass it as a generic to createCookieSessionStorage. No explicit type export from package.

Setup SSR, create typed session storage, and use session in an Astro page to read/write encrypted cookies.

// .env SECRET_KEY_BASE=$(openssl rand -hex 64) // astro.config.mjs import { defineConfig } from 'astro/config'; export default defineConfig({ output: 'server' }); // src/sessions.ts import { createCookieSessionStorage } from 'astro-cookie-session'; type SessionData = { userId: string }; export const { getSession } = createCookieSessionStorage<SessionData>(); // src/pages/index.astro --- import { getSession } from '../sessions'; import type { APIRoute } from 'astro'; if (Astro.request.method === 'POST') { const form = await Astro.request.formData(); const session = getSession(Astro.cookies); session.set('userId', form.get('id') as string); } const session = getSession(Astro.cookies); const userId = session.get('userId'); --- {userId ? <p>Hello, {userId}!</p> : <form method="post"><input type="text" name="id"/><button>Log in</button></form>}
Debug
Known issues
breakingIn v1.3.0, payloads are now encrypted by default. Existing sessions without encryption will fail to decrypt.
fix
Regenerate all session data after upgrading to 1.3.0; old cookies are incompatible.
affects: <1.3.0
gotchaSECRET_KEY_BASE must be exactly 128 hex characters (openssl rand -hex 64 generates correct length). Using a key of different length will cause errors at runtime.
fix
Generate key with: openssl rand -hex 64 and ensure no newline or extra characters.
affects: >=1.0.0
gotchaAstro must be configured with output: 'server' (SSR mode). The middleware will not work in static export mode.
fix
Set output: 'server' in astro.config.mjs.
affects: >=1.0.0
gotchaThe getSession function must be called with Astro.cookies (Astro 4+) or cookies (API routes). Using an incorrect argument will throw a runtime error.
fix
Ensure you pass the correct cookies object: Astro.cookies in pages, or the cookies param in API routes.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Encryption key not set. Please set SECRET_KEY_BASE environment variable.
Missing SECRET_KEY_BASE env variable or not loaded in Astro.
fix
Add SECRET_KEY_BASE to .env file and ensure Astro loads it (e.g., import 'dotenv/config' or use Astro's built-in env support).
TypeError: Cannot destructure property 'getSession' of ... as it is undefined.
Attempting to import createCookieSessionStorage as default import.
fix
Use named import: import { createCookieSessionStorage } from 'astro-cookie-session'
Upgrade
Version history
1.3.0latest on npm
Audit
Dependencies
astrorequiredRequired peer dependency for Astro middleware and cookies API
Agent activity
42 hits · last 30 days
node
35
Amazon
1
OpenAI (training)
1
Resources
astro-cookie-session — npm install astro-cookie-session · libregistry