1 | koa-session [![Build Status](https://secure.travis-ci.org/dead-horse/koa-session.png)](http://travis-ci.org/dead-horse/koa-session) [![Dependency Status](https://gemnasium.com/dead-horse/koa-session.png)](https://gemnasium.com/dead-horse/koa-session)
|
2 | =========
|
3 |
|
4 | koa session with redis
|
5 |
|
6 | [![NPM](https://nodei.co/npm/koa-sess.png?downloads=true)](https://nodei.co/npm/koa-sess/)
|
7 |
|
8 | ## Usage
|
9 |
|
10 | ### Example
|
11 |
|
12 | ```
|
13 | var koa = require('koa');
|
14 | var http = require('http');
|
15 | var session = require('koa-sess');
|
16 | var RedisStore = require('koa-redis');
|
17 |
|
18 | var app = koa();
|
19 | app.keys = ['keys', 'keykeys'];
|
20 | app.use(session({
|
21 | defer: true,
|
22 | store: new RedisStore()
|
23 | }));
|
24 |
|
25 | app.use(function *() {
|
26 | switch (this.request.url) {
|
27 | case '/get':
|
28 | get(this);
|
29 | break;
|
30 | case '/remove':
|
31 | remove(this);
|
32 | break;
|
33 | }
|
34 | });
|
35 |
|
36 | function get(ctx) {
|
37 | var session = yield ctx.session; // defer generate session
|
38 | session.count = session.count || 0;
|
39 | session.count++;
|
40 | var body = session.count;
|
41 | }
|
42 |
|
43 | function remove(ctx) {
|
44 | ctx.session = null;
|
45 | ctx.body = 0;
|
46 | }
|
47 |
|
48 | app.on('error', function (err) {
|
49 | console.error(err.stack);
|
50 | });
|
51 |
|
52 | var app = module.exports = http.createServer(app.callback());
|
53 | app.listen(8080);
|
54 | ```
|
55 |
|
56 | * After add session middlware, you can use `this.session` to set or get the sessions.
|
57 | * set `this.session = null;` will destroy this session.
|
58 | * Alter `this.session.cookie` can handle the cookie options of this user. Also you can use cookie options in session store, for example use `cookie.maxAge` as session store's ttl.
|
59 |
|
60 | ### Options
|
61 |
|
62 | ```
|
63 | *`defer`: defer get session, only generate session when you use it by `var session = yield this.session;`, default is false.
|
64 | *`key` cookie name defaulting to `koa.sid`
|
65 | *`store` session store instance
|
66 | *`cookie` session cookie settings, defaulting to
|
67 | {path: '/', httpOnly: true, maxAge: null, rewrite: true, signed: true}
|
68 | ```
|
69 |
|
70 | * Store can be any Object have `set`, `get`, `destroy` like [MemoryStore](https://github.com/dead-horse/koa-session/blob/master/lib/store.js).
|
71 | * cookie defaulting to
|
72 |
|
73 | ```
|
74 | {
|
75 | path: '/',
|
76 | httpOnly: true,
|
77 | maxAge: null,
|
78 | rewrite: true,
|
79 | signed: true
|
80 | }
|
81 | ```
|
82 |
|
83 | full list of cookie options, see [jed/cookies](https://github.com/jed/cookies#cookiesset-name--value---options--).
|
84 |
|
85 | ## Session Store
|
86 |
|
87 | You can use any other store to replace the default MemoryStore, just need these public api:
|
88 |
|
89 | * `get(sid)`: get session object by sid
|
90 | * `set(sid, sess, ttl)`: set session object for sid, with a ttl (in ms)
|
91 | * `destroy(sid)`: destory session for sid
|
92 |
|
93 | all these api need return a Promise, Thunk or generator.
|
94 |
|
95 | and use these events to report store's status.
|
96 |
|
97 | * `connect`
|
98 | * `disconnect`
|
99 |
|
100 | You can use [koa-redis](https://github.com/dead-horse/koa-redis) to store your session data with redis.
|
101 |
|
102 | ## Licences
|
103 | (The MIT License)
|
104 |
|
105 | Copyright (c) 2013 dead-horse and other contributors
|
106 |
|
107 | 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:
|
108 |
|
109 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
110 |
|
111 | 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.
|
112 |
|
\ | No newline at end of file |