2020-05-20 12:07:58 +03:00
|
|
|
const _ = require('lodash');
|
2020-05-28 21:30:23 +03:00
|
|
|
const logging = require('../../../shared/logging');
|
2019-11-21 06:01:24 +03:00
|
|
|
const membersService = require('./index');
|
2020-05-28 13:57:02 +03:00
|
|
|
const urlUtils = require('../../../shared/url-utils');
|
2020-04-30 21:50:40 +03:00
|
|
|
const ghostVersion = require('../../lib/ghost-version');
|
|
|
|
const settingsCache = require('../settings/cache');
|
2020-05-20 12:07:58 +03:00
|
|
|
const {formattedMemberResponse} = require('./utils');
|
2019-11-21 06:01:24 +03:00
|
|
|
|
2020-04-30 21:33:09 +03:00
|
|
|
// @TODO: This piece of middleware actually belongs to the frontend, not to the member app
|
|
|
|
// Need to figure a way to separate these things (e.g. frontend actually talks to members API)
|
|
|
|
const loadMemberSession = async function (req, res, next) {
|
|
|
|
try {
|
|
|
|
const member = await membersService.ssr.getMemberDataFromSession(req, res);
|
|
|
|
Object.assign(req, {member});
|
|
|
|
res.locals.member = req.member;
|
|
|
|
next();
|
|
|
|
} catch (err) {
|
|
|
|
Object.assign(req, {member: null});
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-26 18:44:25 +03:00
|
|
|
const getIdentityToken = async function (req, res) {
|
2019-11-21 06:01:24 +03:00
|
|
|
try {
|
|
|
|
const token = await membersService.ssr.getIdentityTokenForMemberFromSession(req, res);
|
|
|
|
res.writeHead(200);
|
|
|
|
res.end(token);
|
|
|
|
} catch (err) {
|
|
|
|
res.writeHead(err.statusCode);
|
|
|
|
res.end(err.message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-28 10:53:30 +03:00
|
|
|
const deleteSession = async function (req, res) {
|
2019-11-21 06:01:24 +03:00
|
|
|
try {
|
|
|
|
await membersService.ssr.deleteSession(req, res);
|
|
|
|
res.writeHead(204);
|
|
|
|
res.end();
|
|
|
|
} catch (err) {
|
|
|
|
res.writeHead(err.statusCode);
|
|
|
|
res.end(err.message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-20 09:03:15 +03:00
|
|
|
const getMemberData = async function (req, res) {
|
|
|
|
try {
|
|
|
|
const member = await membersService.ssr.getMemberDataFromSession(req, res);
|
|
|
|
if (member) {
|
2020-05-20 12:07:58 +03:00
|
|
|
res.json(formattedMemberResponse(member));
|
|
|
|
} else {
|
|
|
|
res.json(null);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2021-05-13 12:51:58 +03:00
|
|
|
res.writeHead(204);
|
|
|
|
res.end();
|
2020-05-20 12:07:58 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateMemberData = async function (req, res) {
|
|
|
|
try {
|
2020-05-28 17:01:00 +03:00
|
|
|
const data = _.pick(req.body, 'name', 'subscribed');
|
2020-05-20 12:07:58 +03:00
|
|
|
const member = await membersService.ssr.getMemberDataFromSession(req, res);
|
|
|
|
if (member) {
|
2020-09-04 14:41:04 +03:00
|
|
|
const options = {
|
|
|
|
id: member.id,
|
|
|
|
withRelated: ['stripeSubscriptions', 'stripeSubscriptions.customer']
|
|
|
|
};
|
|
|
|
const updatedMember = await membersService.api.members.update(data, options);
|
|
|
|
|
2020-08-24 11:08:06 +03:00
|
|
|
res.json(formattedMemberResponse(updatedMember.toJSON()));
|
2020-04-20 09:03:15 +03:00
|
|
|
} else {
|
|
|
|
res.json(null);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
res.writeHead(err.statusCode);
|
|
|
|
res.end(err.message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-04 18:32:20 +03:00
|
|
|
const getDefaultProductPrices = async function () {
|
|
|
|
const page = await membersService.api.productRepository.list({
|
|
|
|
limit: 1
|
|
|
|
});
|
|
|
|
const [product] = page.data;
|
|
|
|
if (product) {
|
|
|
|
const model = await membersService.api.productRepository.get({id: product.get('id')}, {withRelated: ['stripePrices']});
|
|
|
|
const productData = model.toJSON();
|
2021-05-07 12:44:56 +03:00
|
|
|
const prices = productData.stripePrices || [];
|
2021-05-07 16:42:12 +03:00
|
|
|
const activePrices = prices.filter((d) => {
|
|
|
|
return !!d.active;
|
|
|
|
});
|
2021-05-07 12:44:56 +03:00
|
|
|
return {
|
|
|
|
product: productData,
|
2021-05-07 16:42:12 +03:00
|
|
|
prices: activePrices
|
2021-05-07 12:44:56 +03:00
|
|
|
};
|
2021-05-04 18:32:20 +03:00
|
|
|
}
|
2021-05-07 12:44:56 +03:00
|
|
|
return {};
|
2021-05-04 18:32:20 +03:00
|
|
|
};
|
|
|
|
|
2020-04-30 21:50:40 +03:00
|
|
|
const getMemberSiteData = async function (req, res) {
|
2020-06-29 17:22:42 +03:00
|
|
|
const isStripeConfigured = membersService.config.isStripeConnected();
|
2020-09-17 10:02:53 +03:00
|
|
|
const domain = urlUtils.urlFor('home', true).match(new RegExp('^https?://([^/:?#]+)(?:[/:?#]|$)', 'i'));
|
2021-01-14 11:49:16 +03:00
|
|
|
const firstpromoterId = settingsCache.get('firstpromoter') ? settingsCache.get('firstpromoter_id') : '';
|
2020-09-17 10:02:53 +03:00
|
|
|
const blogDomain = domain && domain[1];
|
2020-10-15 12:14:03 +03:00
|
|
|
let supportAddress = settingsCache.get('members_support_address') || 'noreply';
|
2020-09-17 10:02:53 +03:00
|
|
|
if (!supportAddress.includes('@')) {
|
|
|
|
supportAddress = `${supportAddress}@${blogDomain}`;
|
|
|
|
}
|
2021-05-07 12:44:56 +03:00
|
|
|
const {product = {}, prices = []} = await getDefaultProductPrices() || {};
|
2020-04-30 21:50:40 +03:00
|
|
|
const response = {
|
|
|
|
title: settingsCache.get('title'),
|
|
|
|
description: settingsCache.get('description'),
|
|
|
|
logo: settingsCache.get('logo'),
|
2020-07-23 14:34:04 +03:00
|
|
|
icon: settingsCache.get('icon'),
|
2020-06-23 15:33:57 +03:00
|
|
|
accent_color: settingsCache.get('accent_color'),
|
2020-04-30 21:50:40 +03:00
|
|
|
url: urlUtils.urlFor('home', true),
|
|
|
|
version: ghostVersion.safe,
|
|
|
|
plans: membersService.config.getPublicPlans(),
|
2021-05-07 12:44:56 +03:00
|
|
|
prices,
|
|
|
|
product: {
|
|
|
|
name: product.name || '',
|
|
|
|
description: product.description || ''
|
|
|
|
},
|
2021-05-07 20:26:16 +03:00
|
|
|
free_price_name: settingsCache.get('members_free_price_name'),
|
|
|
|
free_price_description: settingsCache.get('members_free_price_description'),
|
2020-06-19 17:04:40 +03:00
|
|
|
allow_self_signup: membersService.config.getAllowSelfSignup(),
|
2021-04-27 18:32:22 +03:00
|
|
|
members_signup_access: settingsCache.get('members_signup_access'),
|
2020-06-19 17:04:40 +03:00
|
|
|
is_stripe_configured: isStripeConfigured,
|
|
|
|
portal_button: settingsCache.get('portal_button'),
|
|
|
|
portal_name: settingsCache.get('portal_name'),
|
2020-07-07 11:11:16 +03:00
|
|
|
portal_plans: settingsCache.get('portal_plans'),
|
|
|
|
portal_button_icon: settingsCache.get('portal_button_icon'),
|
|
|
|
portal_button_signup_text: settingsCache.get('portal_button_signup_text'),
|
2020-09-08 18:18:57 +03:00
|
|
|
portal_button_style: settingsCache.get('portal_button_style'),
|
2021-01-14 11:49:16 +03:00
|
|
|
firstpromoter_id: firstpromoterId,
|
2020-09-17 10:02:53 +03:00
|
|
|
members_support_address: supportAddress
|
2020-04-30 21:50:40 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
res.json({site: response});
|
|
|
|
};
|
|
|
|
|
2020-04-29 20:23:55 +03:00
|
|
|
const createSessionFromMagicLink = async function (req, res, next) {
|
2019-11-21 06:01:24 +03:00
|
|
|
if (!req.url.includes('token=')) {
|
|
|
|
return next();
|
|
|
|
}
|
2020-04-29 20:23:55 +03:00
|
|
|
|
2020-05-07 23:55:50 +03:00
|
|
|
// req.query is a plain object, copy it to a URLSearchParams object so we can call toString()
|
|
|
|
const searchParams = new URLSearchParams('');
|
|
|
|
Object.keys(req.query).forEach((param) => {
|
|
|
|
// don't copy the token param
|
|
|
|
if (param !== 'token') {
|
|
|
|
searchParams.set(param, req.query[param]);
|
|
|
|
}
|
|
|
|
});
|
2020-04-29 20:23:55 +03:00
|
|
|
|
2020-05-07 23:55:50 +03:00
|
|
|
try {
|
2020-11-19 12:58:32 +03:00
|
|
|
const member = await membersService.ssr.exchangeTokenForSession(req, res);
|
2021-01-28 20:25:38 +03:00
|
|
|
const subscriptions = member && member.subscriptions || [];
|
2020-11-19 12:58:32 +03:00
|
|
|
|
2021-01-22 15:53:34 +03:00
|
|
|
const action = req.query.action;
|
2020-11-23 12:36:45 +03:00
|
|
|
|
2020-11-19 12:58:32 +03:00
|
|
|
if (action === 'signup') {
|
2020-11-23 12:36:45 +03:00
|
|
|
let customRedirect = '';
|
2020-11-19 12:58:32 +03:00
|
|
|
if (subscriptions.find(sub => ['active', 'trialing'].includes(sub.status))) {
|
2020-11-23 12:36:45 +03:00
|
|
|
customRedirect = settingsCache.get('members_paid_signup_redirect') || '';
|
2020-11-19 12:58:32 +03:00
|
|
|
} else {
|
2020-11-23 12:36:45 +03:00
|
|
|
customRedirect = settingsCache.get('members_free_signup_redirect') || '';
|
2020-11-19 12:58:32 +03:00
|
|
|
}
|
|
|
|
|
2020-11-23 12:36:45 +03:00
|
|
|
if (customRedirect && customRedirect !== '/') {
|
|
|
|
const baseUrl = urlUtils.getSiteUrl();
|
|
|
|
const ensureEndsWith = (string, endsWith) => (string.endsWith(endsWith) ? string : string + endsWith);
|
|
|
|
const removeLeadingSlash = string => string.replace(/^\//, '');
|
2020-11-19 12:58:32 +03:00
|
|
|
|
2020-11-23 12:36:45 +03:00
|
|
|
const redirectUrl = new URL(removeLeadingSlash(ensureEndsWith(customRedirect, '/')), ensureEndsWith(baseUrl, '/'));
|
2020-04-29 20:23:55 +03:00
|
|
|
|
2020-11-23 12:36:45 +03:00
|
|
|
return res.redirect(redirectUrl.href);
|
|
|
|
}
|
2020-11-19 17:47:08 +03:00
|
|
|
}
|
|
|
|
|
2020-11-23 12:36:45 +03:00
|
|
|
// Do a standard 302 redirect to the homepage, with success=true
|
|
|
|
searchParams.set('success', true);
|
|
|
|
res.redirect(`${urlUtils.getSubdir()}/?${searchParams.toString()}`);
|
2019-11-21 06:01:24 +03:00
|
|
|
} catch (err) {
|
2020-04-30 22:26:12 +03:00
|
|
|
logging.warn(err.message);
|
2020-11-19 12:58:32 +03:00
|
|
|
|
|
|
|
// Do a standard 302 redirect to the homepage, with success=false
|
2020-05-08 15:03:44 +03:00
|
|
|
searchParams.set('success', false);
|
2020-11-23 12:36:45 +03:00
|
|
|
res.redirect(`${urlUtils.getSubdir()}/?${searchParams.toString()}`);
|
2019-11-21 06:01:24 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Set req.member & res.locals.member if a cookie is set
|
|
|
|
module.exports = {
|
2020-04-29 20:23:55 +03:00
|
|
|
loadMemberSession,
|
|
|
|
createSessionFromMagicLink,
|
2020-04-21 18:48:42 +03:00
|
|
|
getIdentityToken,
|
2020-04-20 09:03:15 +03:00
|
|
|
getMemberData,
|
2020-05-20 12:07:58 +03:00
|
|
|
updateMemberData,
|
2020-04-30 21:50:40 +03:00
|
|
|
getMemberSiteData,
|
2020-04-21 18:48:42 +03:00
|
|
|
deleteSession,
|
|
|
|
stripeWebhooks: (req, res, next) => membersService.api.middleware.handleStripeWebhook(req, res, next)
|
2019-11-21 06:01:24 +03:00
|
|
|
};
|