diff --git a/addon/mixins/ui-service-mixin.js b/addon/mixins/ui-service-mixin.js index b58a7284..604dd12c 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,34 @@ 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.data === 'getPendingRequestKey') { + messageEvent.source.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 +143,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/package.json b/package.json index 9e6cccc9..ff47b7ff 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "torii", - "version": "1.0.0-beta.2", + "version": "1.0.0-beta.3", "description": "A set of clean abstractions for authentication in Ember.js", "keywords": [ "authentication", diff --git a/public/redirect.html b/public/redirect.html index b58b7a26..cda41a69 100644 --- a/public/redirect.html +++ b/public/redirect.html @@ -4,16 +4,49 @@