diff --git a/core/server/services/members/middleware.js b/core/server/services/members/middleware.js index e4e7c6ba69..d8f2425b83 100644 --- a/core/server/services/members/middleware.js +++ b/core/server/services/members/middleware.js @@ -1,3 +1,4 @@ +const _ = require('lodash'); const {logging} = require('../../lib/common'); const config = require('../../config'); const labsService = require('../labs'); @@ -5,6 +6,7 @@ const membersService = require('./index'); const urlUtils = require('../../lib/url-utils'); const ghostVersion = require('../../lib/ghost-version'); const settingsCache = require('../settings/cache'); +const {formattedMemberResponse} = require('./utils'); // @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) @@ -53,15 +55,24 @@ const getMemberData = async function (req, res) { try { const member = await membersService.ssr.getMemberDataFromSession(req, res); if (member) { - res.json({ - uuid: member.uuid, - email: member.email, - name: member.name, - firstname: member.name && member.name.split(' ')[0], - avatar_image: member.avatar_image, - subscriptions: member.stripe.subscriptions, - paid: member.stripe.subscriptions.length !== 0 - }); + res.json(formattedMemberResponse(member)); + } else { + res.json(null); + } + } catch (err) { + logging.warn(err.message); + res.writeHead(err.statusCode); + res.end(err.message); + } +}; + +const updateMemberData = async function (req, res) { + try { + const data = _.pick(req.body, 'email', 'name', 'subscribed'); + const member = await membersService.ssr.getMemberDataFromSession(req, res); + if (member) { + const updatedMember = await membersService.api.members.update(data, {id: member.id}); + res.json(formattedMemberResponse(updatedMember)); } else { res.json(null); } @@ -129,6 +140,7 @@ module.exports = { createSessionFromMagicLink, getIdentityToken, getMemberData, + updateMemberData, getMemberSiteData, deleteSession, stripeWebhooks: (req, res, next) => membersService.api.middleware.handleStripeWebhook(req, res, next) diff --git a/core/server/services/members/utils.js b/core/server/services/members/utils.js new file mode 100644 index 0000000000..f90cab87d1 --- /dev/null +++ b/core/server/services/members/utils.js @@ -0,0 +1,15 @@ +module.exports.formattedMemberResponse = function formattedMemberResponse(member) { + if (!member) { + return null; + } + return { + uuid: member.uuid, + email: member.email, + name: member.name, + firstname: member.name && member.name.split(' ')[0], + avatar_image: member.avatar_image, + subscribed: !!member.subscribed, + subscriptions: member.stripe ? member.stripe.subscriptions : [], + paid: member.stripe ? member.stripe.subscriptions.length !== 0 : false + }; +}; diff --git a/core/server/web/members/app.js b/core/server/web/members/app.js index 68a0603005..16c6eede2e 100644 --- a/core/server/web/members/app.js +++ b/core/server/web/members/app.js @@ -1,6 +1,8 @@ const debug = require('ghost-ignition').debug('web:members:app'); const {URL} = require('url'); const cors = require('cors'); +const bodyParser = require('body-parser'); +const boolParser = require('express-query-boolean'); const express = require('../../../shared/express'); const urlUtils = require('../../lib/url-utils'); const membersService = require('../../services/members'); @@ -11,6 +13,16 @@ module.exports = function setupMembersApp() { debug('Members App setup start'); const membersApp = express('members'); + // Body parsing + membersApp.use(bodyParser.json({limit: '1mb'})); + membersApp.use(bodyParser.urlencoded({extended: true, limit: '1mb'})); + + // Query parsing + membersApp.use(boolParser()); + + // send 503 json response in case of maintenance + membersApp.use(shared.middlewares.maintenance); + // Entire app is behind labs flag membersApp.use(shared.middlewares.labs.members); @@ -28,6 +40,7 @@ module.exports = function setupMembersApp() { // Initializes members specific routes as well as assigns members specific data to the req/res objects membersApp.get('/api/member', middleware.getMemberData); + membersApp.put('/api/member', middleware.updateMemberData); membersApp.get('/api/session', middleware.getIdentityToken); membersApp.delete('/api/session', middleware.deleteSession); membersApp.get('/api/site', middleware.getMemberSiteData);