# all files / keystone/lib/core/ initExpressSession.js

7.81% Statements 5/64
0% Branches 0/32
0% Functions 0/3
7.94% Lines 5/63

    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
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    1291×
    1×
    1×
    1×

    1×

     var _ = require('lodash');
    var session = require('express-session');
    var cookieParser = require('cookie-parser');
    var debug = require('debug')('keystone:core:initExpressSession');

    module.exports = function initExpressSession () {

    	if (this.expressSession) return this;

    	var sessionStorePromise;

    	// Initialise and validate session options
    	if (!this.get('cookie secret')) {
    		console.error('\nKeystoneJS Configuration Error:\n\nPlease provide a `cookie secret` value for session encryption.\n');
    		process.exit(1);
    	}
    	var sessionOptions = this.get('session options');

    	if (typeof sessionOptions !== 'object') {
    		sessionOptions = {};
    	}
    	if (!sessionOptions.key) {
    		sessionOptions.key = 'this.sid';
    	}
    	if (!sessionOptions.resave) {
    		sessionOptions.resave = false;
    	}
    	if (!sessionOptions.saveUninitialized) {
    		sessionOptions.saveUninitialized = false;
    	}
    	if (!sessionOptions.secret) {
    		sessionOptions.secret = this.get('cookie secret');
    	}

    	sessionOptions.cookieParser = cookieParser(this.get('cookie secret'));

    	var sessionStore = this.get('session store');

    	if (typeof sessionStore === 'function') {
    		sessionOptions.store = sessionStore(session);
    	} else if (sessionStore) {

    		var sessionStoreOptions = this.get('session store options') || {};

    		// Perform any session store specific configuration or exit on an unsupported session store

    		if (sessionStore === 'mongo') {
    			sessionStore = 'connect-mongo';
    		} else if (sessionStore === 'redis') {
    			sessionStore = 'connect-redis';
    		}

    		switch (sessionStore) {
    			case 'connect-mongo':
    				debug('using connect-mongo session store');
    				_.defaults(sessionStoreOptions, {
    					collection: 'app_sessions',
    					url: this.get('mongo'),
    				});
    				break;

    			case 'connect-mongostore':
    				debug('using connect-mongostore session store');
    				_.defaults(sessionStoreOptions, {
    					collection: 'app_sessions',
    				});
    				if (!sessionStoreOptions.db) {
    					console.error(
    						'\nERROR: connect-mongostore requires `session store options` to be set.'
    						+ '\n'
    						+ '\nSee http://thisjs.com/docs/configuration#options-database for details.'
    						+ '\n');
    					process.exit(1);
    				}
    				break;

    			case 'connect-redis':
    				debug('using connect-redis session store');
    				break;

    			default:
    				console.error(
    					'\nERROR: unsupported session store ' + sessionStore + '.'
    					+ '\n'
    					+ '\nSee http://thisjs.com/docs/configuration#options-database for details.'
    					+ '\n');
    				process.exit(1);
    				break;
    		}

    		// Initialize the session store
    		try {
    			var SessionStore = require(sessionStore)(session);

    			sessionStorePromise = new Promise(
    				function (resolve, reject) {
    					sessionOptions.store = new SessionStore(sessionStoreOptions, resolve);
    					sessionOptions.store.on('connect', resolve);
    					sessionOptions.store.on('connected', resolve);
    					sessionOptions.store.on('disconnect', function () {
    						console.error(
    							'\nThere was an error connecting to the ' + sessionStore + ' session store.'
    							+ '\n');
    						process.exit(1);
    					});
    				}
    			);
    		} catch (e) {
    			if (e.code === 'MODULE_NOT_FOUND') {
    				console.error(
    					'\n' + e.toString()
    					+ '\nTo use ' + this.get('session store') + ' as a `session store` option, run:'
    					+ '\nnpm install ' + sessionStore + ' --save'
    					+ '\n');
    				process.exit(1);
    			} else {
    				throw e;
    			}
    		}
    	}

    	// expose initialised session and options
    	this.set('session options', sessionOptions);
    	this.expressSession = session(sessionOptions);
    	this.sessionStorePromise = sessionStorePromise;

    	return this;
    };