Code Monkey home page Code Monkey logo

generic-session's Introduction

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. different from koa-session(it is cookie session).

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.

Usage

Example

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

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;
  }
});

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

  • key: cookie name defaulting to koa.sid

  • prefix: session prefix for store, defaulting to koa:sess:

  • store: session store instance

  • cookie: session cookie settings

  • ttl: ttl is for sessionStore's expiration time. it is different with cookie.maxage, default to null(means get ttl from cookie.maxage).

  • rolling: rolling session, always reset the cookie and sessions, defaults to false

  • genSid: default sid was generated by uid2, you can pass a function to replace it

  • defer: defers get session, only generate a session when you use it through var session = yield this.session;, defaults to false

  • allowEmpty: allow generation of empty sessions

  • 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 expressjs/cookies.

if you setcookie.maxage to null, meaning no "expires" parameter is set so the cookie becomes a browser-session cookie. When the user closes the browser the cookie (and session) will be removed.

Notice that ttl is different from cookie.maxage, ttl set the expire time of sessionStore. So if you set cookie.maxage = null, and ttl=ms('1d'), the session will expired after one day, but the cookie will destory when the user cloese the browser. And mostly you can just ignore options.ttl, koa-generic-session will parse cookie.maxage as the tll.

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

Stores Presented

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.

generic-session's People

Contributors

dead-horse avatar iliakan avatar mekwall avatar nmn avatar pavelvlasov avatar ranadeep47 avatar tb01923 avatar yoshuawuyts avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.