mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Exposed member session data on site
refs https://github.com/TryGhost/members.js/issues/6 This exposes an endpoint on site url (`/members/ssr/member`) to get member's data in exchange for their session/identity on a theme when they are logged in. It essentially uses the same logic and data which is passed down to theme through handlebar helpers, and is used by members.js script to load member data.
This commit is contained in:
parent
dd377492bf
commit
29bc241cf6
2 changed files with 28 additions and 0 deletions
|
@ -42,6 +42,32 @@ const getMemberDataFromSession = async function (req, res, next) {
|
|||
}
|
||||
};
|
||||
|
||||
const getMemberData = async function (req, res) {
|
||||
if (!labsService.isSet('members')) {
|
||||
res.json(null);
|
||||
}
|
||||
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 && req.member.name.split(' ')[0],
|
||||
avatar_image: member.avatar_image,
|
||||
subscriptions: member.stripe.subscriptions,
|
||||
paid: member.stripe.subscriptions.length !== 0
|
||||
});
|
||||
} else {
|
||||
res.json(null);
|
||||
}
|
||||
} catch (err) {
|
||||
common.logging.warn(err.message);
|
||||
res.writeHead(err.statusCode);
|
||||
res.end(err.message);
|
||||
}
|
||||
};
|
||||
|
||||
const exchangeTokenForSession = async function (req, res, next) {
|
||||
if (!labsService.isSet('members')) {
|
||||
return next();
|
||||
|
@ -76,6 +102,7 @@ module.exports = {
|
|||
decorateResponse
|
||||
],
|
||||
getIdentityToken,
|
||||
getMemberData,
|
||||
deleteSession,
|
||||
stripeWebhooks: (req, res, next) => membersService.api.middleware.handleStripeWebhook(req, res, next)
|
||||
};
|
||||
|
|
|
@ -139,6 +139,7 @@ module.exports = function setupSiteApp(options = {}) {
|
|||
|
||||
// Members middleware
|
||||
// Initializes members specific routes as well as assigns members specific data to the req/res objects
|
||||
siteApp.get('/members/ssr/member', shared.middlewares.labs.members, membersMiddleware.getMemberData);
|
||||
siteApp.get('/members/ssr', shared.middlewares.labs.members, membersMiddleware.getIdentityToken);
|
||||
siteApp.delete('/members/ssr', shared.middlewares.labs.members, membersMiddleware.deleteSession);
|
||||
siteApp.post('/members/webhooks/stripe', shared.middlewares.labs.members, membersMiddleware.stripeWebhooks);
|
||||
|
|
Loading…
Add table
Reference in a new issue