1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | var Cookie = require('./cookie')
|
16 | var EventEmitter = require('events').EventEmitter
|
17 | var Session = require('./session')
|
18 | var util = require('util')
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | module.exports = Store
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | function Store () {
|
33 | EventEmitter.call(this)
|
34 | }
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | util.inherits(Store, EventEmitter)
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | Store.prototype.regenerate = function(req, fn){
|
51 | var self = this;
|
52 | this.destroy(req.sessionID, function(err){
|
53 | self.generate(req);
|
54 | fn(err);
|
55 | });
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | Store.prototype.load = function(sid, fn){
|
68 | var self = this;
|
69 | this.get(sid, function(err, sess){
|
70 | if (err) return fn(err);
|
71 | if (!sess) return fn();
|
72 | var req = { sessionID: sid, sessionStore: self };
|
73 | fn(null, self.createSession(req, sess))
|
74 | });
|
75 | };
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | Store.prototype.createSession = function(req, sess){
|
87 | var expires = sess.cookie.expires
|
88 | var originalMaxAge = sess.cookie.originalMaxAge
|
89 |
|
90 | sess.cookie = new Cookie(sess.cookie);
|
91 |
|
92 | if (typeof expires === 'string') {
|
93 |
|
94 | sess.cookie.expires = new Date(expires)
|
95 | }
|
96 |
|
97 |
|
98 | sess.cookie.originalMaxAge = originalMaxAge
|
99 |
|
100 | req.session = new Session(req, sess);
|
101 | return req.session;
|
102 | };
|