Added member identity and site data APIs

refs https://github.com/TryGhost/members.js/issues/6

To self-contain all the data needs for members.js, we load the site data and member's logged in state using available APIs instead of passing them down the theme
- Member Identity API uses member cookie and `/ssr` endpoint to identify if a member is currently logged in or not
- Site data API is the public admin API endpoint for fetching public site data like title, description, logo, brand etc.
This commit is contained in:
Rish 2020-04-17 14:50:18 +05:30
parent 4ba70683a8
commit 0c0e1069ae

View File

@ -29,19 +29,47 @@ function createSendMagicLinkApi(adminUrl) {
if (res.ok) {
return 'Success';
} else {
console.log('Failed to send magic link!', res);
return 'Failed to send magic link';
}
});
};
}
function createMemberIdentityApi(siteUrl) {
return function () {
return fetch(`${siteUrl}/members/ssr`, {
credentials: 'same-origin'
}).then(function (res) {
if (!res.ok) {
return null;
}
return res.text();
});
};
}
function createSiteDataApi(adminUrl) {
return function () {
return fetch(`${adminUrl}/api/canary/admin/site/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}).then(function (res) {
if (res.ok) {
return res.json();
} else {
return 'Failed to fetch site data';
}
});
};
}
function createCheckoutPlanApi(siteUrl, adminUrl) {
return function ({plan, checkoutCancelUrl, checkoutSuccessUrl}) {
return fetch(`${siteUrl}/members/ssr`, {
credentials: 'same-origin'
}).then(function (res) {
console.log('Checkout Plan Response', res, res.ok);
if (!res.ok) {
return null;
}
@ -84,7 +112,9 @@ function setupMembersApi({siteUrl, adminUrl}) {
return {
sendMagicLink: createSendMagicLinkApi(adminUrl),
signout: createSignoutApi(siteUrl),
checkoutPlan: createCheckoutPlanApi(siteUrl, adminUrl)
checkoutPlan: createCheckoutPlanApi(siteUrl, adminUrl),
getMemberIdentity: createMemberIdentityApi(siteUrl),
getSiteData: createSiteDataApi(adminUrl)
};
}