You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.4 KiB

  1. /*!
  2. * Connect - session - Session
  3. * Copyright(c) 2010 Sencha Inc.
  4. * Copyright(c) 2011 TJ Holowaychuk
  5. * MIT Licensed
  6. */
  7. /**
  8. * Module dependencies.
  9. */
  10. var utils = require('../../utils');
  11. /**
  12. * Create a new `Session` with the given request and `data`.
  13. *
  14. * @param {IncomingRequest} req
  15. * @param {Object} data
  16. * @api private
  17. */
  18. var Session = module.exports = function Session(req, data) {
  19. Object.defineProperty(this, 'req', { value: req });
  20. Object.defineProperty(this, 'id', { value: req.sessionID });
  21. if ('object' == typeof data) utils.merge(this, data);
  22. };
  23. /**
  24. * Update reset `.cookie.maxAge` to prevent
  25. * the cookie from expiring when the
  26. * session is still active.
  27. *
  28. * @return {Session} for chaining
  29. * @api public
  30. */
  31. Session.prototype.touch = function(){
  32. return this.resetMaxAge();
  33. };
  34. /**
  35. * Reset `.maxAge` to `.originalMaxAge`.
  36. *
  37. * @return {Session} for chaining
  38. * @api public
  39. */
  40. Session.prototype.resetMaxAge = function(){
  41. this.cookie.maxAge = this.cookie.originalMaxAge;
  42. return this;
  43. };
  44. /**
  45. * Save the session data with optional callback `fn(err)`.
  46. *
  47. * @param {Function} fn
  48. * @return {Session} for chaining
  49. * @api public
  50. */
  51. Session.prototype.save = function(fn){
  52. this.req.sessionStore.set(this.id, this, fn || function(){});
  53. return this;
  54. };
  55. /**
  56. * Re-loads the session data _without_ altering
  57. * the maxAge properties. Invokes the callback `fn(err)`,
  58. * after which time if no exception has occurred the
  59. * `req.session` property will be a new `Session` object,
  60. * although representing the same session.
  61. *
  62. * @param {Function} fn
  63. * @return {Session} for chaining
  64. * @api public
  65. */
  66. Session.prototype.reload = function(fn){
  67. var req = this.req
  68. , store = this.req.sessionStore;
  69. store.get(this.id, function(err, sess){
  70. if (err) return fn(err);
  71. if (!sess) return fn(new Error('failed to load session'));
  72. store.createSession(req, sess);
  73. fn();
  74. });
  75. return this;
  76. };
  77. /**
  78. * Destroy `this` session.
  79. *
  80. * @param {Function} fn
  81. * @return {Session} for chaining
  82. * @api public
  83. */
  84. Session.prototype.destroy = function(fn){
  85. delete this.req.session;
  86. this.req.sessionStore.destroy(this.id, fn);
  87. return this;
  88. };
  89. /**
  90. * Regenerate this request's session.
  91. *
  92. * @param {Function} fn
  93. * @return {Session} for chaining
  94. * @api public
  95. */
  96. Session.prototype.regenerate = function(fn){
  97. this.req.sessionStore.regenerate(this.req, fn);
  98. return this;
  99. };