koa-header-session
v1.1.1
Published
koa header session store by memory, redis or others
Downloads
1
Readme
koa-header-session
Header session middleware for koa, easy to use with custom stores such as redis or mongo, supports defer session getter. based on koa-generic-session(it is a header based session).
This middleware will only set a header when a session is manually set. Each time the session is modified (and only when the is session modified), it will reset the header and session.
You can use the rolling sessions that will reset the header and session for every request which touch the session.
Usage
Example
var session = require('koa-header-session');
var redisStore = require('koa-redis');
var koa = require('koa');
var app = koa();
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.
Options
header
: HTTP header used for session ids, defaulting toX-Session-ID
path
: Allowed path for sessions, defaulting to/
prefix
: session prefix for store, defaulting tokoa:sess:
store
: session store instancettl
: ttl is for sessionStore's expiration time. it is different withcookie.maxage
, default to null(means get ttl fromcookie.maxage
).rolling
: rolling session, always reset the cookie and sessions, defaults tofalse
genSid
: default sid was generated by uid2, you can pass a function to replace itdefer
: defers get session, only generate a session when you use it throughvar session = yield this.session;
, defaults tofalse
allowEmpty
: allow generation of empty sessionserrorHandler(err, type, ctx)
:Store.get
andStore.set
will throw in some situation, useerrorHandle
to handle these errors by yourself. Default will throw.Store can be any Object that has the methods
set
,get
,destroy
like MemoryStore.
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 sidset(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
- koa-redis to store your session data with redis.
- koa-mysql-session to store your session data with MySQL.
- koa-generic-session-mongo to store your session data with MongoDB.
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.