hiddentao / koa-session-store

Session middleware for Koa with a pluggable storage layer
12 stars 2 forks source link

koa-session-store

Build Status NPM module

Session middleware for Koa with a pluggable storage layer (for use with Mongo, Redis, etc.). Based on koa-session.

Installation

npm install koa-session-store

Usage

var session = require('koa-session-store');
var koa = require('koa');

var app = koa();

app.keys = ['some secret key'];  // needed for cookie-signing

// cookie will be named "koa:sess" and session data will be stored in the cookie itself
app.use(session());

app.use(function(next){
  return function *(){
    var n = this.session.views || 0;
    this.session.views = ++n;
    this.body = n + ' views';
  }
})

app.listen(3000);
console.log('listening on port 3000');

To delete an existing session:

this.session = null;

The following configuration options are available:

app.use(session({
  name: 'mysite'    // cookie name
  store: <object instance> or "cookie"   // session storage layer - see below
  cookie: {
    // cookie configuration options - see below
  }
}));

The default cookie configuration options are set to:

signed: true      // cookie is signed using KeyGrip
httpOnly: true    // cookie is not accessible via client-side JS
overwrite: true   // overwrite existing cookie datawhen setting cookie

For a full list of options see the cookies module.

Session storage layer

The store configuration option specifies where the session data is stored. If omitted or set to "cookie" then session data will be stored in the cookie itself.

If you wish to store session data elsewhere (e.g. in Mongo, Redis, etc.) then you must set this to an object which exposes the following API:

The following storage layers are currently available:

License

Copyright (c) 2013-2014 Ramesh Nair

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.