开发者社区> 镜像站> NPM> koa-sess
koa-sess

koa session store by memory, redis or others

koa-sess is no more maintained, please use koa-generic-session to replace
Last updated 6 years ago by dead_horse .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install koa-sess 
SYNC missed versions from official npm registry.

koa-session Build Status

THIS MODULE RENAMED TO KOA-GENERIC-SESSION IN NPM.

session middlewares fro koa, easy use with custom stores such as redis, 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 is session 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.

NPM

Usage

Example


var koa = require('koa');
var session = require('koa-sess');
var RedisStore = require('koa-redis');

var app = koa();
app.keys = ['keys', 'keykeys'];
app.use(session({
  store: new RedisStore()
}));

app.use(function *() {
  switch (this.path) {
  case '/get':
    get.call(this);
    break;
  case '/remove':
    remove.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;
}

app.listen(8080);
  • After adding session middleware, you can use this.session to set or get the sessions.
  • Setting this.session = null; will destroy this session.
  • Altering this.session.cookie changes the cookie options of this user. Also you can use the cookie options in session the store. Use for example cookie.maxage as the session store's ttl.

Options

 *`defer`: defers get session, only generate a session when you use it through `var session = yield this.session;`, defaults to `false`
 *`key`: cookie name defaulting to `koa.sid`
 *`prefix`: session prefix for store, defaulting to `koa:sess:`
 *`store`: session store instance
 *`cookie`: session cookie settings, defaults to
    {path: '/', httpOnly: true, maxage: null, rewrite: true, signed: true}
 * `rolling`: rolling session, always reset the cookie and sessions, defaults to `false`
 * `genSid`: default sid was generated by [uid2](https://github.com/coreh/uid2), you can pass a function to replace it
  • Store can be any Object that has the methods set, get, destroy like MemoryStore.
  • cookie defaulting to
{
  path: '/',
  httpOnly: true,
  maxage: null,
  rewrite: true,
  signed: true
}

For a full list of cookie options see jed/cookies.

Session Store

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

  • get(sid): get session object by sid
  • set(sid, sess, ttl): set session object for sid, with a ttl (in ms)
  • destroy(sid): destory session for sid

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

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

  • connect
  • disconnect

You can use koa-redis to store your session data with redis.

Licences

(The MIT License)

Copyright (c) 2013 - 2014 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.

Current Tags

  • 0.4.1                                ...           latest (6 years ago)

19 Versions

  • 0.4.1 [deprecated]           ...           6 years ago
  • 0.4.0                                ...           6 years ago
  • 0.3.3                                ...           7 years ago
  • 0.3.2                                ...           7 years ago
  • 0.3.1                                ...           7 years ago
  • 0.3.0                                ...           7 years ago
  • 0.2.1                                ...           7 years ago
  • 0.2.0                                ...           7 years ago
  • 0.1.0                                ...           7 years ago
  • 0.1.0-beta1                                ...           7 years ago
  • 0.0.9                                ...           7 years ago
  • 0.0.8                                ...           7 years ago
  • 0.0.7                                ...           7 years ago
  • 0.0.6                                ...           7 years ago
  • 0.0.5                                ...           7 years ago
  • 0.0.4                                ...           7 years ago
  • 0.0.3                                ...           7 years ago
  • 0.0.2                                ...           7 years ago
  • 0.0.1                                ...           7 years ago
Maintainers (1)
Downloads
Today 0
This Week 2
This Month 2
Last Day 0
Last Week 1
Last Month 20
Dependencies (4)
Dev Dependencies (10)