diff --git a/ghost/sdk/layer2/index.js b/ghost/sdk/layer2/index.js index 718c62cbf2..0f2e0eb91d 100644 --- a/ghost/sdk/layer2/index.js +++ b/ghost/sdk/layer2/index.js @@ -3,12 +3,14 @@ var layer1 = require('@tryghost/members-layer1'); module.exports = function layer2(options) { var authUrl = `${options.membersUrl}/auth`; var gatewayUrl = `${options.membersUrl}/gateway`; + var container = options.container; 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.width = '100%'; frame.style.height = '100%'; @@ -27,43 +29,36 @@ module.exports = function layer2(options) { function openAuth(hash, query = '') { return loadAuth.then(function (frame) { - frame.src = `${authUrl}#${hash}?${query}`; - frame.style.display = 'block'; - window.addEventListener('message', function self(event) { - if (event.source !== frame.contentWindow) { - return; - } - if (event.data !== 'pls-close-auth-popup') { - return; - } - window.removeEventListener('message', self); - frame.style.display = 'none'; - }) - return frame; + return new Promise(function (resolve) { + frame.src = `${authUrl}#${hash}?${query}`; + frame.style.display = 'block'; + window.addEventListener('message', function messageListener(event) { + if (event.source !== frame.contentWindow) { + return; + } + if (event.data !== 'pls-close-auth-popup') { + return; + } + window.removeEventListener('message', messageListener); + frame.style.display = 'none'; + resolve(false); + }) + members.bus.on('signedin', function signedinListener() { + members.bus.off('signedin', signedinListener); + frame.style.display = 'none'; + resolve(true); + }); + }); }); } function resetPassword({token}) { const query = `token=${token}`; - return openAuth('reset-password', query).then(function () { - return new Promise(function (resolve) { - members.bus.on('signedin', function self() { - members.bus.off('signedin', self); - resolve(true); - }); - }); - }); + return openAuth('reset-password', query); } function signin() { - return openAuth('signin').then(function () { - return new Promise(function (resolve) { - members.bus.on('signedin', function self() { - members.bus.off('signedin', self); - resolve(true); - }); - }); - }); + return openAuth('signin'); } function getToken({audience}) { diff --git a/ghost/sdk/theme-dropin/src/index.js b/ghost/sdk/theme-dropin/src/index.js index 8efa6f665b..a446c04476 100644 --- a/ghost/sdk/theme-dropin/src/index.js +++ b/ghost/sdk/theme-dropin/src/index.js @@ -2,8 +2,10 @@ const DomReady = require('domready'); const GhostContentApi = require('@tryghost/content-api'); const layer2 = require('@tryghost/members-layer2'); -function reload() { - window.location.reload(); +function reload(success) { + if (success) { + window.location.reload(); + } } 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-_]+)/) || []; if (tokenMatch) { return members.resetPassword({token}) - .then(() => { + .then((success) => { window.location.hash = ''; + return success; }) .then(reload); }