Ghost/core/server/services/members/index.js
Fabien O'Carroll b8fc0d2bd1
Cached member data in ghost-members-ssr-cache cookie (#11096)
no-issue

* Installed @tryghost/members-ssr@0.4.0
  This now supports caching of the data returned by the members-api

* Renamed cookies set by members-ssr
  As discussed with @ErisDS I have prefixed these cookies with `ghost`
2019-09-09 17:39:46 +08:00

54 lines
1.7 KiB
JavaScript

const MembersSSR = require('@tryghost/members-ssr');
const createMembersApiInstance = require('./api');
const common = require('../../lib/common');
const urlUtils = require('../../lib/url-utils');
const settingsCache = require('../settings/cache');
let membersApi;
// Bind to events to automatically keep subscription info up-to-date from settings
common.events.on('settings.edited', function updateSettingFromModel(settingModel) {
if (!['members_subscription_settings'].includes(settingModel.get('key'))) {
return;
}
const reconfiguredMembersAPI = createMembersApiInstance();
reconfiguredMembersAPI.bus.on('ready', function () {
membersApi = reconfiguredMembersAPI;
});
reconfiguredMembersAPI.bus.on('error', function (err) {
common.logging.error(err);
});
});
const membersService = {
isPaymentConfigured() {
const settings = settingsCache.get('members_subscription_settings');
return !!settings && settings.isPaid && settings.paymentProcessors.length !== 0;
},
get api() {
if (!membersApi) {
membersApi = createMembersApiInstance();
membersApi.bus.on('error', function (err) {
common.logging.error(err);
});
}
return membersApi;
},
ssr: MembersSSR({
cookieSecure: urlUtils.isSSL(urlUtils.getSiteUrl()),
cookieKeys: [settingsCache.get('theme_session_secret')],
cookieName: 'ghost-members-ssr',
cookieCacheName: 'ghost-members-ssr-cache',
// This is passed as a function so that updates to the instance
// are picked up in the ssr module
membersApi: () => membersApi
})
};
module.exports = membersService;