2019-07-18 10:37:11 +03:00
|
|
|
const MembersSSR = require('@tryghost/members-ssr');
|
|
|
|
|
|
|
|
const createMembersApiInstance = require('./api');
|
2020-05-28 21:30:23 +03:00
|
|
|
const {events} = require('../../lib/common');
|
|
|
|
const logging = require('../../../shared/logging');
|
2020-05-28 13:57:02 +03:00
|
|
|
const urlUtils = require('../../../shared/url-utils');
|
2019-07-18 10:37:11 +03:00
|
|
|
const settingsCache = require('../settings/cache');
|
|
|
|
|
|
|
|
let membersApi;
|
|
|
|
|
|
|
|
// Bind to events to automatically keep subscription info up-to-date from settings
|
2020-04-30 22:26:12 +03:00
|
|
|
events.on('settings.edited', function updateSettingFromModel(settingModel) {
|
2019-09-06 10:14:21 +03:00
|
|
|
if (!['members_subscription_settings'].includes(settingModel.get('key'))) {
|
2019-07-18 10:37:11 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const reconfiguredMembersAPI = createMembersApiInstance();
|
|
|
|
reconfiguredMembersAPI.bus.on('ready', function () {
|
|
|
|
membersApi = reconfiguredMembersAPI;
|
|
|
|
});
|
|
|
|
reconfiguredMembersAPI.bus.on('error', function (err) {
|
2020-04-30 22:26:12 +03:00
|
|
|
logging.error(err);
|
2019-07-18 10:37:11 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const membersService = {
|
2019-11-05 05:47:12 +03:00
|
|
|
contentGating: require('./content-gating'),
|
|
|
|
|
2019-11-07 13:13:20 +03:00
|
|
|
config: require('./config'),
|
|
|
|
|
2018-12-11 10:57:01 +03:00
|
|
|
get api() {
|
2019-07-18 10:37:11 +03:00
|
|
|
if (!membersApi) {
|
|
|
|
membersApi = createMembersApiInstance();
|
|
|
|
|
|
|
|
membersApi.bus.on('error', function (err) {
|
2020-04-30 22:26:12 +03:00
|
|
|
logging.error(err);
|
2019-07-18 10:37:11 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return membersApi;
|
2019-05-08 15:08:25 +03:00
|
|
|
},
|
|
|
|
|
2019-07-18 10:37:11 +03:00
|
|
|
ssr: MembersSSR({
|
|
|
|
cookieSecure: urlUtils.isSSL(urlUtils.getSiteUrl()),
|
|
|
|
cookieKeys: [settingsCache.get('theme_session_secret')],
|
2019-09-09 12:39:46 +03:00
|
|
|
cookieName: 'ghost-members-ssr',
|
|
|
|
cookieCacheName: 'ghost-members-ssr-cache',
|
2019-09-16 10:58:10 +03:00
|
|
|
getMembersApi: () => membersService.api
|
2019-09-03 07:39:00 +03:00
|
|
|
})
|
2018-12-11 10:57:01 +03:00
|
|
|
};
|
2019-07-18 10:37:11 +03:00
|
|
|
|
|
|
|
module.exports = membersService;
|
2019-11-21 06:01:24 +03:00
|
|
|
module.exports.middleware = require('./middleware');
|