<commit_before># koa-session2

[![NPM version][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]
[![Downloads][downloads-image]][downloads-url]

Middleware for [Koa2](https://github.com/koajs/koa) to get/set session use with custom stores such as Redis or mongodb

Use native ES6(async/await) by Nodejs v7.x, use `--harmony` option.

Or you can use the old versions:
- [babel](https://github.com/Secbone/koa-session2/tree/babel)
- [node6](https://github.com/Secbone/koa-session2/tree/node6)

## Require
node v7.x +

## Install
```
npm install koa-session2
```

## Usage
```js
const Koa = require("koa");
const session = require("koa-session2");

const app = new Koa();

app.use(session({
    key: "SESSIONID",   //default "koa:sess"
}));
```

### Custom Stores

Store.js
```js
const Redis = require("ioredis");
const Store = require("koa-session2/libs/store");

class RedisStore extends Store {
    constructor() {
        super();
        this.redis = new Redis();
    }

    async get(sid) {
        let data = await this.redis.get(`SESSION:${sid}`);
        return JSON.parse(data);
    }

    async set(session, opts) {
        if(!opts.sid) {
            opts.sid = this.getID(24);
        }
        await this.redis.set(`SESSION:${opts.sid}`, JSON.stringify(session));
        return opts.sid;
    }

    async destroy(sid) {
        return await this.redis.del(`SESSION:${sid}`);
    }
}

module.exports = RedisStore;
```
main.js
```js
const Koa = require("koa");
const session = require("koa-session2");
const Store = require("./Store.js");

const app = new Koa();

app.use(session({
    store: new Store()
}));

app.use(ctx => {
    let user = ctx.session.user;
<commit_msg>updated docs with using setEx at Redis<commit_after># koa-session2

[![NPM version][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]
[![Downloads][downloads-image]][downloads-url]

Middleware for [Koa2](https://github.com/koajs/koa) to get/set session use with custom stores such as Redis or mongodb

Use native ES6(async/await) by Nodejs v7.x, use `--harmony` option.

Or you can use the old versions:
- [babel](https://github.com/Secbone/koa-session2/tree/babel)
- [node6](https://github.com/Secbone/koa-session2/tree/node6)

## Require
node v7.x +

## Install
```
npm install koa-session2
```

## Usage
```js
const Koa = require("koa");
const session = require("koa-session2");

const app = new Koa();

app.use(session({
    key: "SESSIONID",   //default "koa:sess"
}));
```

### Custom Stores

Store.js
```js
const Redis = require("ioredis");
const { Store } = require("koa-session2");

class RedisStore extends Store {
    constructor() {
        super();
        this.redis = new Redis();
    }

    async get(sid) {
        let data = await this.redis.get(`SESSION:${sid}`);
        return JSON.parse(data);
    }

    async set(session, { sid =  this.getID(24), maxAge = 1000000 } = {}) {
        try {
            // Use redis set EX to automatically drop expired sessions
            await this.redis.set(`SESSION:${sid}`, JSON.stringify(session), 'EX', maxAge / 1000);
        } catch (e) {}
        return sid;
    }

    async destroy(sid) {
        return await this.redis.del(`SESSION:${sid}`);
    }
}

module.exports = RedisStore;
```
main.js
```js
const Koa = require("koa");
const session = require("koa-session2");
const Store = require("./Store.js");

const app = new Koa();

app.use(session({
    store: new Store()
}));

app.use(ctx => {
    let user = ctx.session.user;

{'fraction_non_alphanumeric': 0.17673888255416192, 'fraction_numerical': 0.011687571265678449, 'mean_word_length': 3.2897310513447433, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1580162', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1233, 'n_words': 285}