mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
Added member update endpoint with session auth (#11824)
- Allows member logged in with valid session to update their profile info - name, email, subscribed(newsletter subscription status) - Adds new util method for formatted member response on the endpoints - Adds common middlewares for body/bool parser and maintenance - Adds `subscribed` status to member response
This commit is contained in:
parent
c2470bcddb
commit
fe3eab1836
3 changed files with 49 additions and 9 deletions
|
@ -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)
|
||||
|
|
15
core/server/services/members/utils.js
Normal file
15
core/server/services/members/utils.js
Normal file
|
@ -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
|
||||
};
|
||||
};
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue