Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support external store #66

Merged
merged 21 commits into from
Feb 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "eslint-config-egg",
}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
sudo: false
language: node_js
node_js:
- '0.12'
- '4'
- '6'
- '7'
script: 'npm run test-travis'
after_script: 'npm install coveralls@2 && cat ./coverage/lcov.info | coveralls'
27 changes: 15 additions & 12 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ app.use(convert(session(app)));
// codes
```

## Semantics

This module provides "guest" sessions, meaning any visitor will have a session,
authenticated or not. If a session is _new_ a Set-Cookie will be produced regardless
of populating the session.

## API

### Options
Expand All @@ -104,6 +98,21 @@ app.use(convert(session(app)));
- `valid()`: valid session value before use it
- `beforeSave()`: hook before save session

### External Session Stores

Session will store in cookie by default, but it has some disadvantages:

- Session stored in client side unencrypted.
- [Browser cookie always have length limit](http://browsercookielimits.squawky.net/).

You can store the session content in external stores(redis, mongodb or other DBs) by pass `options.store` with three methods(need to be generator function or async function):

- `get(key)`: get session object by key
- `set(key, sess, maxAge)`: set session object for key, with a `maxAge` (in ms)
- `destroy(key)`: destroy session for key

Once you passed `options.store`, session is strong dependent on your external store, you can't access session if your external store is down. **Use external session stores only if necessary, avoid use session as a cache, keep session lean and stored by cookie!**

### Session#isNew

Returns __true__ if the session is new.
Expand Down Expand Up @@ -132,12 +141,6 @@ if (this.session.isNew) {
this.session = null;
```

## Session Stores

This module only supports cookie sessions. There are many other modules listed in [koa's wiki](https://github.com/koajs/koa/wiki#wiki-sessions) for sessions that use database storage. Unlike Connect 2.x's session middleware, there is no main "session" middleware that you plugin different stores - each store is a completely different module.

If you're interested in creating your own koa session store, feel free to fork/extend this repository and add additional tests. At a minimum, it __should__ pass this repositories' tests that apply. Ideally, there would be a central repository with specifications and tests for all koa sessions, which would allow interoperability and consistency between session modules. If you're interested in working on such a project, let us know!

## License

MIT
Loading