2019-10-14 18:23:29 +00:00
|
|
|
import { addSuccessHandler } from 'ember-service-worker/service-worker-registration';
|
|
|
|
import Namespace from '@ember/application/namespace';
|
|
|
|
|
|
|
|
function getToken() {
|
|
|
|
// fix this later by allowing registration somewhere in the app lifecycle were we can have access to
|
|
|
|
// services, etc.
|
|
|
|
return Namespace.NAMESPACES_BY_ID['vault'].__container__.lookup('service:auth').currentToken;
|
|
|
|
}
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
addSuccessHandler(function (registration) {
|
2019-10-14 18:23:29 +00:00
|
|
|
// attach the handler for the message event so we can send over the auth token
|
2021-12-17 03:44:29 +00:00
|
|
|
navigator.serviceWorker.addEventListener('message', (event) => {
|
2019-10-14 18:23:29 +00:00
|
|
|
let { action } = event.data;
|
|
|
|
let port = event.ports[0];
|
|
|
|
|
|
|
|
if (action === 'getToken') {
|
|
|
|
let token = getToken();
|
|
|
|
if (!token) {
|
|
|
|
console.error('Unable to retrieve Vault tokent');
|
|
|
|
}
|
|
|
|
port.postMessage({ token: token });
|
|
|
|
} else {
|
|
|
|
console.error('Unknown event', event);
|
|
|
|
port.postMessage({
|
|
|
|
error: 'Unknown request',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// attempt to unregister the service worker on unload because we're not doing any sort of caching
|
2021-12-17 03:44:29 +00:00
|
|
|
window.addEventListener('unload', function () {
|
2019-10-14 18:23:29 +00:00
|
|
|
registration.unregister();
|
|
|
|
});
|
|
|
|
});
|