-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodule.js
78 lines (58 loc) · 2.43 KB
/
module.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var injector = require('injector')
, session = require('express-session')
, passport = require('passport')
, cookieParser = require('cookie-parser')
, Module = require('classes').Module;
var AuthModule = Module.extend({
sessionStore: null,
preSetup: function () {
var env = process.env.NODE_ENV ? process.env.NODE_ENV : 'local'
, storeDriver = this.config.store;
this.debug('Using ' + storeDriver + ' as store...');
if (storeDriver === 'redis') {
this.setupRedisSessionStore(env, this.config.redis);
} else if (storeDriver === 'memcache') {
this.setupMemcacheSessionStore(env, this.config.memcache);
}
},
setupRedisSessionStore: function(env, redisConfig) {
this.debug('Configuring connect-redis for use as session storage: ' + JSON.stringify(redisConfig));
redisConfig.prefix = !!redisConfig.prefix ? redisConfig.prefix + env + '_' : env + '_';
this.sessionStore = new (require('connect-redis')(session))(redisConfig);
},
setupMemcacheSessionStore: function(env, memcacheConfig) {
this.debug('Configuring connect-memcached for use as session storage: ' + JSON.stringify(memcacheConfig));
memcacheConfig.prefix = !!memcacheConfig.prefix ? memcacheConfig.prefix + env + '_' : env + '_';
this.sessionStore = new (require('connect-memcached')(session))(memcacheConfig);
},
preInit: function() {
this.debug('Adding passport and sessionStore to the injector...');
injector.instance('passport', passport);
injector.instance('sessionStore', this.sessionStore);
},
configureApp: function(app) {
this.debug('Configuring express to use the cookieParser...');
app.use(cookieParser());
this.debug('Configuring session management...');
var sessionConfig = {
secret: this.config.secretKey,
cookie: { secure: false, maxAge: 86400000 },
resave: false,
saveUninitialized: false
};
if (this.config.store !== 'in-memory') {
sessionConfig.store = this.sessionStore;
}
app.use(session(sessionConfig));
this.debug('Configuring passport initialize middleware...');
app.use(passport.initialize());
this.debug('Configuring passport session middleware..');
app.use(passport.session());
this.emit('appReady');
},
preShutdown: function () {
this.debug('Closing session store connection...');
this.sessionStore.client.quit();
}
});
module.exports = AuthModule;