koa-generic-session
Advanced tools
Comparing version 1.6.0 to 1.7.0
1.6.0 / 2015-01-16 | ||
1.7.0 / 2015-03-01 | ||
================== | ||
* Add support for regenerating sessions. | ||
1.6.0 / 2015-01-16 | ||
================== | ||
* Merge pull request #39 from rfink/sess_id_optional_check | ||
* Allow a forced session id that is not available in the cookie | ||
1.5.0 / 2015-01-07 | ||
1.5.0 / 2015-01-07 | ||
================== | ||
@@ -10,0 +15,0 @@ |
@@ -232,2 +232,18 @@ /**! | ||
this.session = result.session; | ||
this.regenerateSession = function *regenerateSession() { | ||
debug('regenerating session'); | ||
if (!result.isNew) { | ||
// destroy the old session | ||
debug('destroying previous session'); | ||
yield store.destroy(this.sessionId); | ||
} | ||
this.session = generateSession(); | ||
this.sessionId = genSid.call(this, 24); | ||
this.cookies.set(key, null); | ||
debug('created new session: %s', this.sessionId); | ||
result.isNew = true; | ||
} | ||
yield *next; | ||
@@ -284,2 +300,21 @@ yield *refreshSession.call(this, this.session, result.originalHash, result.isNew); | ||
this.regenerateSession = function *regenerateSession() { | ||
debug('regenerating session'); | ||
// make sure that the session has been loaded | ||
yield this.session; | ||
if (!isNew) { | ||
// destroy the old session | ||
debug('destroying previous session'); | ||
yield store.destroy(this.sessionId); | ||
} | ||
this._session = generateSession(); | ||
this.sessionId = genSid.call(this, 24); | ||
this.cookies.set(key, null); | ||
debug('created new session: %s', this.sessionId); | ||
isNew = true; | ||
return this._session; | ||
} | ||
yield *next; | ||
@@ -286,0 +321,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"repository": "koajs/generic-session", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"keywords": ["koa", "middleware", "session"], | ||
@@ -8,0 +8,0 @@ "author": "dead_horse <dead_horse@qq.com>", |
@@ -57,2 +57,5 @@ generic-session | ||
break; | ||
case '/regenerate': | ||
yield regenerate.call(this); | ||
break; | ||
} | ||
@@ -73,2 +76,8 @@ }); | ||
function *regenerate() { | ||
get.call(this); | ||
yield this.regenerateSession(); | ||
get.call(this); | ||
} | ||
app.listen(8080); | ||
@@ -80,2 +89,3 @@ ``` | ||
* Altering `this.session.cookie` changes the cookie options of this user. Also you can use the cookie options in session the store. Use for example `cookie.maxage` as the session store's ttl. | ||
* Calling `this.regenerateSession` will destroy any existing session and generate a new, empty one in its place. The new session will have a different ID. | ||
@@ -82,0 +92,0 @@ ### Options |
@@ -151,3 +151,25 @@ /**! | ||
}); | ||
it('should regenerate existing sessions', function (done) { | ||
var agent = request.agent(app) | ||
agent | ||
.get('/session/get') | ||
.expect(/.+/, function(err, res) { | ||
var firstId = res.body; | ||
agent | ||
.get('/session/regenerate') | ||
.expect(/.+/, function(err, res) { | ||
var secondId = res.body; | ||
secondId.should.not.equal(firstId); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should regenerate new sessions', function (done) { | ||
request(app) | ||
.get('/session/regenerateWithData') | ||
.expect({ /* foo: undefined, */ hasSession: true }, done); | ||
}); | ||
}); | ||
}); |
@@ -181,3 +181,25 @@ /**! | ||
}); | ||
it('should regenerate existing sessions', function (done) { | ||
var agent = request.agent(app) | ||
agent | ||
.get('/session/get') | ||
.expect(/.+/, function(err, res) { | ||
var firstId = res.body; | ||
agent | ||
.get('/session/regenerate') | ||
.expect(/.+/, function(err, res) { | ||
var secondId = res.body; | ||
secondId.should.not.equal(firstId); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should regenerate new sessions', function (done) { | ||
request(app) | ||
.get('/session/regenerateWithData') | ||
.expect({ /* foo: undefined, */ hasSession: true }, done); | ||
}); | ||
}); | ||
}); |
@@ -75,2 +75,11 @@ /**! | ||
break; | ||
case '/session/regenerate': | ||
yield regenerate(this); | ||
break; | ||
case '/session/regenerateWithData': | ||
var session = yield this.session; | ||
session.foo = 'bar'; | ||
session = yield regenerate(this); | ||
this.body = { foo : session.foo, hasSession: session !== undefined }; | ||
break; | ||
default: | ||
@@ -113,4 +122,11 @@ yield other(this); | ||
function *regenerate(ctx) { | ||
var session = yield ctx.regenerateSession(); | ||
session.data = 'foo'; | ||
ctx.body = ctx.sessionId; | ||
return session; | ||
} | ||
// app.listen(7001) | ||
var app = module.exports = http.createServer(app.callback()); | ||
app.store = store; |
@@ -66,3 +66,3 @@ /**! | ||
case '/favicon.ico': | ||
this.staus = 404; | ||
this.status = 404; | ||
break; | ||
@@ -94,2 +94,10 @@ case '/wrongpath': | ||
break; | ||
case '/session/regenerate': | ||
yield regenerate(this); | ||
break; | ||
case '/session/regenerateWithData': | ||
this.session.foo = 'bar'; | ||
yield regenerate(this); | ||
this.body = { foo: this.session.foo, hasSession: this.session !== undefined }; | ||
break; | ||
default: | ||
@@ -129,3 +137,9 @@ other(this); | ||
function *regenerate(ctx) { | ||
yield ctx.regenerateSession(); | ||
ctx.session.data = 'foo'; | ||
getId(ctx); | ||
} | ||
var app = module.exports = http.createServer(app.callback()); | ||
app.store = store; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48497
1214
155