koajs / generic-session

koa session store with memory, redis or others.
MIT License
414 stars 65 forks source link

Documentation regarding options.key and the cookie name not clear #62

Closed mrooding closed 4 years ago

mrooding commented 8 years ago

The documentation regarding the use of options.key and the generation of the cookie name could be more clear and it would be nice if it used the same kind of fallback to options.cookie.name as options.ttl does.

I had the following scenario and assumed that this would work:

app.use(session({
    cookie: {
        name: 'mytnt-addressbook',
        maxAge: 1000 * 60 * 60,
        path: '/',
        httpOnly: true,
        rewrite: true,
        signed: true
    },
    store: redisStorage({
        host: settings.redis.host
    })
}));

After some debugging I found out that if you only supply the cookie.name without the key property it will fail and recreate a session for every request. The cookie name is being picked up by the cookie.name property but the retrieval and storage of the session relies on the key property which defaults to 'koa.sid'.

I read the part about the ttl property being based on the cookie max age if you do not supply the ttl property. I would suggest doing the same for the key. If key is not supplied and cookie.name is set, fall back to that, otherwise fall back to the default 'koa.sid'.

I'm willing to do a PR to change this and update the read me accordingly.