Secbone / koa-session2

Middleware for Koa2 to get/set session
MIT License
152 stars 30 forks source link

Fixed Bug #19

Closed shhdgit closed 7 years ago

shhdgit commented 7 years ago

There some different between babel version and node6 version, that make node6 version set a new session every time.

babel:

    await next();

    // if not changed
    if(old == JSON.stringify(ctx.session)) return;   ** finish **

    // clear old session if exists
    if(id) {
        await opts.store.destroy(id);
        id = null;
    }

    // set new session
    if(ctx.session && Object.keys(ctx.session).length) {
        let sid = await opts.store.set(ctx.session, Object.assign({}, opts, {sid: id}));
        ctx.cookies.set(opts.key, sid, opts);
    }

node6:

    promise.then(() => {
        old = JSON.stringify(ctx.session);

        return next();
    }).then(() => {
        // no modify
        if(old == JSON.stringify(ctx.session)) return;

        // destory old session
        if(id) return opts.store.destroy(id);

    }).then(() => {                             ** always through **
        // clear id
        id = null;

        if(ctx.session && Object.keys(ctx.session).length) {
            // set new session
            return opts.store.set(ctx.session, Object.assign({}, opts, {sid: id})).then(sid => {
                ctx.cookies.set(opts.key, sid, opts)
            });
        }
    });
codecov-io commented 7 years ago

Current coverage is 91.83% (diff: 100%)

Merging #19 into node6 will not change coverage

@@              node6        #19   diff @@
==========================================
  Files             2          2          
  Lines            49         49          
  Methods           7          7          
  Messages          0          0          
  Branches         10         10          
==========================================
  Hits             45         45          
  Misses            4          4          
  Partials          0          0          

Powered by Codecov. Last update af1986a...ad5f841

Secbone commented 7 years ago

@shhdgit I got the problem, but did you have any other way to fix it than throwing an error?

shhdgit commented 7 years ago

@Secbone https://github.com/xieranmaya/blog/issues/5 这篇文章能给一些灵感?优雅一点,也复杂一点。