Updated members sdks for third party use (#25)

* Pass container from layer2 for iframes to be attached

* Updated layer2 to resolve with success for auth page methods

* Updated theme dropin to reload when auth method succeeds
This commit is contained in:
Fabien O'Carroll 2018-12-10 15:55:10 +07:00 committed by GitHub
parent b990761136
commit 51be84182d
2 changed files with 32 additions and 34 deletions

View File

@ -3,12 +3,14 @@ var layer1 = require('@tryghost/members-layer1');
module.exports = function layer2(options) { module.exports = function layer2(options) {
var authUrl = `${options.membersUrl}/auth`; var authUrl = `${options.membersUrl}/auth`;
var gatewayUrl = `${options.membersUrl}/gateway`; var gatewayUrl = `${options.membersUrl}/gateway`;
var container = options.container;
var members = layer1({ var members = layer1({
gatewayUrl gatewayUrl,
container
}); });
var loadAuth = loadFrame(authUrl).then(function (frame) { var loadAuth = loadFrame(authUrl, container).then(function (frame) {
frame.style.position = 'fixed'; frame.style.position = 'fixed';
frame.style.width = '100%'; frame.style.width = '100%';
frame.style.height = '100%'; frame.style.height = '100%';
@ -27,43 +29,36 @@ module.exports = function layer2(options) {
function openAuth(hash, query = '') { function openAuth(hash, query = '') {
return loadAuth.then(function (frame) { return loadAuth.then(function (frame) {
return new Promise(function (resolve) {
frame.src = `${authUrl}#${hash}?${query}`; frame.src = `${authUrl}#${hash}?${query}`;
frame.style.display = 'block'; frame.style.display = 'block';
window.addEventListener('message', function self(event) { window.addEventListener('message', function messageListener(event) {
if (event.source !== frame.contentWindow) { if (event.source !== frame.contentWindow) {
return; return;
} }
if (event.data !== 'pls-close-auth-popup') { if (event.data !== 'pls-close-auth-popup') {
return; return;
} }
window.removeEventListener('message', self); window.removeEventListener('message', messageListener);
frame.style.display = 'none'; frame.style.display = 'none';
resolve(false);
}) })
return frame; members.bus.on('signedin', function signedinListener() {
members.bus.off('signedin', signedinListener);
frame.style.display = 'none';
resolve(true);
});
});
}); });
} }
function resetPassword({token}) { function resetPassword({token}) {
const query = `token=${token}`; const query = `token=${token}`;
return openAuth('reset-password', query).then(function () { return openAuth('reset-password', query);
return new Promise(function (resolve) {
members.bus.on('signedin', function self() {
members.bus.off('signedin', self);
resolve(true);
});
});
});
} }
function signin() { function signin() {
return openAuth('signin').then(function () { return openAuth('signin');
return new Promise(function (resolve) {
members.bus.on('signedin', function self() {
members.bus.off('signedin', self);
resolve(true);
});
});
});
} }
function getToken({audience}) { function getToken({audience}) {

View File

@ -2,8 +2,10 @@ const DomReady = require('domready');
const GhostContentApi = require('@tryghost/content-api'); const GhostContentApi = require('@tryghost/content-api');
const layer2 = require('@tryghost/members-layer2'); const layer2 = require('@tryghost/members-layer2');
function reload() { function reload(success) {
if (success) {
window.location.reload(); window.location.reload();
}
} }
function show (el) { function show (el) {
@ -24,8 +26,9 @@ DomReady(function () {
const [tokenMatch, token] = query.match(/token=([a-zA-Z0-9-_]+.[a-zA-Z0-9-_]+.[a-zA-Z0-9-_]+)/) || []; const [tokenMatch, token] = query.match(/token=([a-zA-Z0-9-_]+.[a-zA-Z0-9-_]+.[a-zA-Z0-9-_]+)/) || [];
if (tokenMatch) { if (tokenMatch) {
return members.resetPassword({token}) return members.resetPassword({token})
.then(() => { .then((success) => {
window.location.hash = ''; window.location.hash = '';
return success;
}) })
.then(reload); .then(reload);
} }