Skip to content

Commit

Permalink
Refactor regenerate as session signal _requireRegenerate.
Browse files Browse the repository at this point in the history
  • Loading branch information
palmtale authored and Cartoon Zhang committed Jun 18, 2017
1 parent dcede0c commit 919968a
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 13 deletions.
7 changes: 0 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,6 @@ function extendContext(context, opts) {
return this[_CONTEXT_SESSION];
},
},
regenerateSession: {
get() {
return async() => {
await this[_CONTEXT_SESSION].regenerate();
};
},
},
session: {
get() {
return this[CONTEXT_SESSION].get();
Expand Down
11 changes: 5 additions & 6 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,6 @@ class ContextSession {
this.session = new Session(this.ctx, val);
}

async regenerate() {
await this.remove();
this.create();
this.session.save();
}

/**
* Commit the session changes or removal.
*
Expand All @@ -207,6 +201,11 @@ class ContextSession {
await this.remove();
return;
}
if (session._requireRegenerate) {
await this.remove();
if (this.opts.store) this.externalKey = uid.sync(24);
session.save();
}

// force save session when `session._requireSave` set
if (!session._requireSave) {
Expand Down
4 changes: 4 additions & 0 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ class Session {
save() {
this._requireSave = true;
}

regenerate() {
this._requireRegenerate = true;
}
}

module.exports = Session;
42 changes: 42 additions & 0 deletions test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,48 @@ describe('Koa Session Cookie', () => {
});
});

describe('ctx.session.regenerate', () => {
it('should change the session key, but not content', done => {
const app = new App();
const message = 'hi';
app.use(async function(ctx, next) {
ctx.session = { message: 'hi' };
await next();
});

app.use(async function(ctx, next) {
const sessionKey = ctx.cookies.get('koa:sess');
if (sessionKey) {
await ctx.session.regenerate();
}
await next();
});

app.use(async function(ctx) {
ctx.session.message.should.equal(message);
ctx.body = '';
});
let koaSession = null;
request(app.callback())
.get('/')
.expect(200, (err, res) => {
should.not.exist(err);
koaSession = res.headers['set-cookie'][0];
koaSession.should.containEql('koa:sess=');
request(app.callback())
.get('/')
.set('Cookie', koaSession)
.expect(200, (err, res) => {
should.not.exist(err);
const cookies = res.headers['set-cookie'][0];
cookies.should.containEql('koa:sess=');
cookies.should.not.equal(koaSession);
done();
});
});
});
});

describe('when get session before enter session middleware', () => {
it('should work', done => {
const app = new Koa();
Expand Down
42 changes: 42 additions & 0 deletions test/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,48 @@ describe('Koa Session External Store', () => {
});
});

describe('ctx.session.regenerate', () => {
it('should change the session key, but not content', done => {
const app = new App();
const message = 'hi';
app.use(async function(ctx, next) {
ctx.session = { message: 'hi' };
await next();
});

app.use(async function(ctx, next) {
const sessionKey = ctx.cookies.get('koa:sess');
if (sessionKey) {
await ctx.session.regenerate();
}
await next();
});

app.use(async function(ctx) {
ctx.session.message.should.equal(message);
ctx.body = '';
});
let koaSession = null;
request(app.callback())
.get('/')
.expect(200, (err, res) => {
should.not.exist(err);
koaSession = res.headers['set-cookie'][0];
koaSession.should.containEql('koa:sess=');
request(app.callback())
.get('/')
.set('Cookie', koaSession)
.expect(200, (err, res) => {
should.not.exist(err);
const cookies = res.headers['set-cookie'][0];
cookies.should.containEql('koa:sess=');
cookies.should.not.equal(koaSession);
done();
});
});
});
});

describe('when store return empty', () => {
it('should create new Session', done => {
const app = App({ signed: false });
Expand Down

0 comments on commit 919968a

Please sign in to comment.