yoho-session.js 2.45 KB
const _ = require('lodash');
const uuid = require('uuid');
const config = global.yoho.config;
const session = require('yoho-express-session');
const cookieSession = require('client-sessions');
const redis = require('connect-redis');
const RedisStore = redis(session);

/**
 * 该中间件主要把 express-session 和 client-session 集中起来处理,如果 redis 出错了,使用 cookie session
 * @param opts.backSession cookieSession 的键名
 * @returns {function(*=, *=, *)}
 */
function yohoSession(opts) {
    return (req, res, next) => {
        let notUseRedis = _.get(req.app.locals.pc, 'session.removeMemcached', false); // 开关字段暂时复用原字段名

        opts.backSession = opts.backSession || 'session2';
        if (req.session && !notUseRedis) {
            req.sessionError = false;
        } else {
            // 重建 session
            res.emit('sessionError');
            req.sessionError = true;

            req.sessionID = req.sessionID || uuid.v4();
            req.session = new session.Session(req, req[opts.backSession].sessionBack);
            req.session.cookie = new session.Cookie({
                domain: config.cookieDomain,
                httpOnly: false
            });
        }

        Object.defineProperty(req.session, 'reset', {
            configurable: true,
            enumerable: false,
            value: function() {
                req.session.destroy();
                req[opts.backSession].reset();
            },
            writable: false
        });

        // 备份数据
        req[opts.backSession].sessionBack = req.session;

        next();
    };
}

module.exports = (app) => {
    app.use(session({  // eslint-disable-line
        proxy: true,
        resave: false,
        saveUninitialized: true,
        unset: 'destroy',
        secret: '82dd7e724f2c6870472c89dfa43cf48d',
        name: 'yohobuy_session',
        cookie: {
            domain: config.cookieDomain,
            httpOnly: true
        },
        getid() {
            return uuid.v4();
        },
        store: new RedisStore(config.redis.session)
    }));

    app.use(cookieSession({ // eslint-disable-line
        requestKey: 'session2',
        cookieName: 'yohobuy_session_cookie',
        secret: '82dd7e724f2c6870472c89dfa43cf48d',
        cookie: {
            domain: config.cookieDomain,
            ephemeral: true,
            httpOnly: true
        }
    }));

    app.use(yohoSession({
        backSession: 'session2'
    }));
};