common/helpers/session.js

"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _get3 = _interopRequireDefault(require("lodash/get"));
var _debug = _interopRequireDefault(require("debug"));
var _connectRedis = _interopRequireDefault(require("connect-redis"));
var _config = _interopRequireDefault(require("./config"));
var _cache = _interopRequireDefault(require("./cache"));
var _expressSession = _interopRequireDefault(require("express-session"));
var debug = (0, _debug.default)('bbsite');

/**
 * Set up sessions, using Redis in production and default to in-memory for testing environment.
 * @param {string} [environment] - Node.js environment.
 * @returns {function} Express session request handler.
 */
function setupSessions(environment) {
  var sessionOptions = {
    cookie: {
      maxAge: (0, _get3.default)(_config.default, 'session.maxAge', 2592000000),
      secure: (0, _get3.default)(_config.default, 'session.secure', false)
    },
    resave: false,
    saveUninitialized: false,
    secret: _config.default.session.secret
  };
  if (environment !== 'test') {
    _cache.default.connect().catch(function (redisError) {
      return debug('Redis error:', redisError);
    });
    _cache.default.on('error', function (redisError) {
      return debug('Redis error:', redisError);
    });
    var redisStore = new _connectRedis.default({
      client: _cache.default
    });
    sessionOptions.store = redisStore;
  }
  return (0, _expressSession.default)(sessionOptions);
}
var _default = setupSessions;
exports.default = _default;
//# sourceMappingURL=session.js.map