mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
Lazily instantiated express-session middleware
refs https://github.com/TryGhost/Team/issues/756 When running the tests it was possible for this middleware to be instantiated before the settings cache, resulting in an undefined 'session_secret' setting being passed. This would cause tests to fail. Tracking this down proved difficult, so the fix was made here, by instantiating the express-session middleware only once a request needs to use it, we can be confident that Ghost has completely started.
This commit is contained in:
parent
5ea8e9b926
commit
62ee693310
1 changed files with 21 additions and 13 deletions
|
@ -9,7 +9,11 @@ const urlUtils = require('../../../../shared/url-utils');
|
||||||
const SessionStore = require('./store');
|
const SessionStore = require('./store');
|
||||||
const sessionStore = new SessionStore(models.Session);
|
const sessionStore = new SessionStore(models.Session);
|
||||||
|
|
||||||
const expressSessionMiddleware = session({
|
let unoExpressSessionMiddleware;
|
||||||
|
|
||||||
|
function getExpressSessionMiddleware() {
|
||||||
|
if (!unoExpressSessionMiddleware) {
|
||||||
|
unoExpressSessionMiddleware = session({
|
||||||
store: sessionStore,
|
store: sessionStore,
|
||||||
secret: settingsCache.get('session_secret'),
|
secret: settingsCache.get('session_secret'),
|
||||||
resave: false,
|
resave: false,
|
||||||
|
@ -22,12 +26,16 @@ const expressSessionMiddleware = session({
|
||||||
sameSite: 'lax',
|
sameSite: 'lax',
|
||||||
secure: urlUtils.isSSL(config.get('url'))
|
secure: urlUtils.isSSL(config.get('url'))
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
return unoExpressSessionMiddleware;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports.getSession = async function getSession(req, res) {
|
module.exports.getSession = async function getSession(req, res) {
|
||||||
if (req.session) {
|
if (req.session) {
|
||||||
return req.session;
|
return req.session;
|
||||||
}
|
}
|
||||||
|
const expressSessionMiddleware = getExpressSessionMiddleware();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
expressSessionMiddleware(req, res, function (err) {
|
expressSessionMiddleware(req, res, function (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue