Generic session middleware for Koa framework (current stable v2.3.1). Provides flexible session management with pluggable external stores (redis, mongo, etc.) and supports synchronous and generator-based session handling. Key differentiator: it only writes cookies when session data changes, supports rolling sessions, and exposes session ID via this.sessionId. Requires Node >=6 and Koa v2, with async/await support. Maintained by the Koa core team.
npm install koa-generic-sessionNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows basic session setup with Koa v2, external Redis store, incrementing a counter, and destroying a session.
Upgrade to Koa v2 and use async middleware. For Koa v1, stay on koa-generic-session@1.x.
Set `rolling: true` in options to reset cookie and TTL on every response that touches the session.
To also clear cookie, explicitly set ctx.cookies.set(key, null) or use ctx.session = null and then manually delete cookie.
Set cookie.maxAge for browser cookie expiration, and ttl for store data expiration. They can be different.
Replace MemoryStore with a persistent store or memory store from another package.
Use `const session = require('koa-generic-session')` (not destructured) or `import session from 'koa-generic-session'`Run `npm install koa-redis` (or your chosen store) and ensure it is included in package.json
Use `await ctx.regenerateSession()` in async functions (v2) or `yield this.regenerateSession()` in generator functions (v1)
Ensure session middleware is used before routes that call ctx.saveSession()