Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-session

Package Overview
Dependencies
Maintainers
8
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-session

Koa cookie session middleware with external store support

  • 6.4.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
188K
decreased by-11.01%
Maintainers
8
Weekly downloads
 
Created

What is koa-session?

koa-session is a session middleware for Koa, a popular web framework for Node.js. It provides a way to manage user sessions, including storing session data, setting session cookies, and handling session expiration.

What are koa-session's main functionalities?

Basic Session Setup

This code sets up a basic Koa application with session management. It configures the session middleware with various options like cookie key, max age, and httpOnly flag. The middleware is then used to track the number of views for each session.

const Koa = require('koa');
const session = require('koa-session');
const app = new Koa();

app.keys = ['some secret hurr'];
const CONFIG = {
  key: 'koa:sess', // cookie key (default is koa:sess)
  maxAge: 86400000, // cookie's max age in ms (1 day)
  autoCommit: true, // automatically commit headers (default true)
  overwrite: true, // can overwrite or not (default true)
  httpOnly: true, // httpOnly or not (default true)
  signed: true, // signed or not (default true)
  rolling: false, // Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown.
  renew: false, // renew session when session is nearly expired, so we can always keep user logged in.
};

app.use(session(CONFIG, app));

app.use(ctx => {
  if (ctx.path === '/favicon.ico') return;
  let n = ctx.session.views || 0;
  ctx.session.views = ++n;
  ctx.body = n + ' views';
});

app.listen(3000);

Custom Session Store

This code demonstrates how to use a custom session store with koa-session. The custom store is implemented using a simple in-memory Map. The store object provides methods for getting, setting, and destroying session data.

const Koa = require('koa');
const session = require('koa-session');
const app = new Koa();

app.keys = ['some secret hurr'];

const store = {
  storage: new Map(),
  get(key) {
    return this.storage.get(key);
  },
  set(key, sess) {
    this.storage.set(key, sess);
  },
  destroy(key) {
    this.storage.delete(key);
  }
};

const CONFIG = {
  store,
};

app.use(session(CONFIG, app));

app.use(ctx => {
  if (ctx.path === '/favicon.ico') return;
  let n = ctx.session.views || 0;
  ctx.session.views = ++n;
  ctx.body = n + ' views';
});

app.listen(3000);

Session Regeneration

This code shows how to regenerate a session in koa-session. When the user accesses the '/regenerate' path, the session is regenerated, which can be useful for security purposes, such as after a user logs in.

const Koa = require('koa');
const session = require('koa-session');
const app = new Koa();

app.keys = ['some secret hurr'];
const CONFIG = {};

app.use(session(CONFIG, app));

app.use(async ctx => {
  if (ctx.path === '/favicon.ico') return;
  if (ctx.path === '/regenerate') {
    await ctx.regenerateSession();
    ctx.body = 'Session regenerated';
  } else {
    let n = ctx.session.views || 0;
    ctx.session.views = ++n;
    ctx.body = n + ' views';
  }
});

app.listen(3000);

Other packages similar to koa-session

Keywords

FAQs

Package last updated on 04 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc