Ghost/core/server/services/members/index.js
Fabien O'Carroll 90d582c5f6 Added config module to members service
no-issue

This is for a central place to retrive member related settings from
2019-11-08 17:21:20 +07:00

56 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;
},
contentGating: require('./content-gating'),
config: require('./config'),
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',
getMembersApi: () => membersService.api
})
};
module.exports = membersService;