1
0
Fork 0

Implemented session.getExpiry, session.hasExpired.

This commit is contained in:
Alan Plum 2014-08-27 10:29:44 +02:00
parent 4d31fe1bb0
commit 9489a3cd50
2 changed files with 29 additions and 8 deletions

View File

@ -6,7 +6,7 @@ The sessions app provides a session storage JavaScript API that can be used in o
This app has the following configuration options:
* *timeToLive* (optional): number of milliseconds until the session expires. Default: *604800000* (one week).
* *timeToLive* (optional): number of milliseconds until the session expires or *0* to disable session expiry. Default: *604800000* (one week).
* *ttlType* (optional): attribute against which the *timeToLive* is enforced. Valid options: *lastAccess*, *lastUpdate*, *created*. Default: *"created"*.
* *sidTimestamp* (optional): whether to append a timestamp to the random part of generated session IDs. Default: *false*.
* *sidLength* (optional): number of random characters to use for new session IDs. Default *20*.
@ -222,6 +222,22 @@ assertEqual(session.get('uid'), user.get('_key'));
assertEqual(session.get('userData'), user.get('userData'));
```
!SUBSECTION Determine whether a session has expired
Get a session's expiry state.
`session.hasExpired()`
Returns *true* if the session's expiry time lies in the past, *false* otherwise.
!SUBSECTION Determine when a session will expire
Get a session's expiry time.
`session.getExpiry()`
Returns an integer representing the time at which the session will expire, or *Number.MAX_VALUE* (indicating the session will never expire) if session expiry is disabled.
!SUBSECTION Add a session cookie to a response
Add a session cookie to a Foxx response.

View File

@ -127,17 +127,22 @@
_.extend(Session.prototype, {
enforceTimeout: function () {
if (!cfg.timeToLive) {
return;
if (this.hasExpired()) {
throw new errors.SessionExpired(this.get('_key'));
}
var now = Number(new Date()),
prop = cfg.ttlType;
},
hasExpired: function () {
return Date.now() > this.getExpiry();
},
getExpiry: function () {
if (!cfg.timeToLive) {
return Number.MAX_VALUE;
}
var prop = cfg.ttlType;
if (!prop || !this.get(prop)) {
prop = 'created';
}
if (cfg.timeToLive < (now - this.get(prop))) {
throw new errors.SessionExpired(this.get('_key'));
}
return this.get(prop) + cfg.timeToLive;
},
addCookie: function (res, cookieName, secret) {
var value = this.get('_key'),