2020-04-06 12:49:14 +03:00
|
|
|
const session = require('express-session');
|
|
|
|
const constants = require('../../../lib/constants');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../../shared/config');
|
2020-04-06 12:49:14 +03:00
|
|
|
const settingsCache = require('../../settings/cache');
|
|
|
|
const models = require('../../../models');
|
2020-05-28 13:57:02 +03:00
|
|
|
const urlUtils = require('../../../../shared/url-utils');
|
2020-04-06 12:49:14 +03:00
|
|
|
|
|
|
|
const SessionStore = require('./store');
|
|
|
|
|
|
|
|
const expressSessionMiddleware = session({
|
|
|
|
store: new SessionStore(models.Session),
|
|
|
|
secret: settingsCache.get('session_secret'),
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: false,
|
|
|
|
name: 'ghost-admin-api-session',
|
|
|
|
cookie: {
|
|
|
|
maxAge: constants.SIX_MONTH_MS,
|
|
|
|
httpOnly: true,
|
|
|
|
path: urlUtils.getSubdir() + '/ghost',
|
|
|
|
sameSite: 'lax',
|
|
|
|
secure: urlUtils.isSSL(config.get('url'))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
exports.getSession = async function getSession(req, res) {
|
|
|
|
if (req.session) {
|
|
|
|
return req.session;
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
expressSessionMiddleware(req, res, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
resolve(req.session);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|