Home

Awesome

generic-session

NPM version build status Coveralls David deps node version npm download Gittip

Generic session middleware for koa, easy use with custom stores such as redis or mongo, supports defer session getter.

This middleware will only set a cookie when a session is manually set. Each time the session is modified (and only when the session is modified), it will reset the cookie and session.

You can use the rolling sessions that will reset the cookie and session for every request which touch the session. Save behavior can be overridden per request.

For async/await and Node v6.9.0+ support use v2.x of this package, for older use v1.x

Usage

Example


var session = require('koa-generic-session');
var redisStore = require('koa-redis');
var koa = require('koa');

var app = new koa(); // for koa v1 use `var app = koa();`
app.keys = ['keys', 'keykeys'];
app.use(session({
  store: redisStore()
}));

app.use(function *() {
  switch (this.path) {
  case '/get':
    get.call(this);
    break;
  case '/remove':
    remove.call(this);
    break;
  case '/regenerate':
    yield regenerate.call(this);
    break;
  }
});

function get() {
  var session = this.session;
  session.count = session.count || 0;
  session.count++;
  this.body = session.count;
}

function remove() {
  this.session = null;
  this.body = 0;
}

function *regenerate() {
  get.call(this);
  yield this.regenerateSession();
  get.call(this);
}

app.listen(8080);

Options

Hooks

Session Store

You can use any other store to replace the default MemoryStore, it just needs to follow this api:

the api needs to return a Promise, Thunk or generator.

And use these events to report the store's status.

Stores Presented

Graceful shutdown

Since this middleware comes with an auto-reconnect feature, it's very likely you can't gracefully shutdown after closing the client as generic-session will try to recover the connection, in those cases you can disable reconnect feature (https://github.com/koajs/generic-session/blob/49b45612877d1a1b8a42dc61bfeba46b71f9cb52/src/session.js#L103-L112) desactivating the client emitter (do this only when stopping the server)

Example with ioredis

  // ...disconnecting from db
  redisClient.emit = () => true;
  await redisClient.quit();
  // ...stopping the server

Licences

(The MIT License)

Copyright (c) 2013 - 2016 dead-horse and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.