koajs / session

Simple session middleware for koa
MIT License
902 stars 113 forks source link

store sets new cookie each response #129

Closed djrolleyes closed 6 years ago

djrolleyes commented 6 years ago

Here is the code. What exactly should I do to get correct key to set it into the Redis DB?


var Redis = require('ioredis'),  
redis = new Redis({db: 3});

const session = require('koa-session');

app.keys = ['some secret hurr'];

const CONFIG = {
  key: 'koa:sess',
  maxAge: 86400000,
  overwrite: true,
  httpOnly: true,
  signed: true,
  rolling: false,
  renew: false,

  store: {

    get: async function(key, maxAge, { rolling }){

      console.log(key, maxAge, rolling )

    },

    set: async function(key, sess, maxAge, { rolling, changed }){

      await redis.hset(key, "views", sess.views);

    },

    destroy: async function(key){

      console.log('destroy')
    }
  }
};

app.use(session(CONFIG, app));

router.get('/session', async(ctx, next) => {

  ctx.set('Access-Control-Allow-Origin', '*');

  if (ctx.path === '/favicon.ico') return;

  let n = ctx.session.views || 0;
  ctx.session.views = ++n;

  ctx.body = ctx.session.views;
});```
dead-horse commented 6 years ago

maybe you can try https://github.com/koajs/koa-redis

djrolleyes commented 6 years ago

@dead-horse Well, koa-redis uses koa-generic-session, and when you get this page at github there is a message: koa-session support external store now, please migrate to koa-session. Anyways, it's increadably interesting to know how exactly should I use store object to store session object in Redis.

dead-horse commented 6 years ago

koa-redis also support koa-session, and if you want to know how to store session in redis, just checkout koa-redis source code