diff --git a/addon/mixins/ui-service-mixin.js b/addon/mixins/ui-service-mixin.js index b58a7284..b098ff2e 100644 --- a/addon/mixins/ui-service-mixin.js +++ b/addon/mixins/ui-service-mixin.js @@ -38,6 +38,7 @@ var ServicesMixin = Mixin.create({ let service = this; let lastRemote = this.remote; let storageToriiEventHandler; + let messageToriiEventHandler; return new EmberPromise(function (resolve, reject) { if (lastRemote) { @@ -55,7 +56,36 @@ var ServicesMixin = Mixin.create({ }); } }; + + // Using postMessage as an alternative to localStorage/storageEvent + // for case of web site embedded in iframe + messageToriiEventHandler = function (messageEvent) { + if (messageEvent.source === service.remote) { + if (messageEvent.data === 'getPendingRequestKey') { + service.remote.postMessage( + JSON.stringify({ pendingRequestKey: service.pendingRequestKey }), + window.location.origin + ); + } else { + const msg = JSON.parse(messageEvent.data); + const key = Object.keys(msg)[0]; + var remoteIdFromEvent = PopupIdSerializer.deserialize( + decodeURIComponent(key) + ); + if (remoteId === remoteIdFromEvent) { + var data = parseMessage(msg[key], keys); + localStorage.removeItem(key); + run(function () { + resolve(data); + }); + } + } + } + }; + window.addEventListener('message', messageToriiEventHandler); + var pendingRequestKey = PopupIdSerializer.serialize(remoteId); + service.pendingRequestKey = pendingRequestKey; localStorage.setItem(CURRENT_REQUEST_KEY, pendingRequestKey); localStorage.removeItem(WARNING_KEY); @@ -115,6 +145,7 @@ var ServicesMixin = Mixin.create({ }).finally(function () { // didClose will reject this same promise, but it has already resolved. service.close(); + window.removeEventListener('message', messageToriiEventHandler); window.removeEventListener('storage', storageToriiEventHandler); }); }, diff --git a/public/redirect.html b/public/redirect.html index b58b7a26..ca7d1cdc 100644 --- a/public/redirect.html +++ b/public/redirect.html @@ -4,16 +4,43 @@ Torii OAuth Redirect