Ohm-Management - Projektarbeit B-ME
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.

session.js 3.0KB

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