UNPKG

codogo-react-widgets

Version:

Provides a unified way to access the styling of commonly used widgets across different apps

81 lines (58 loc) 2.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _jwtDecode = require("jwt-decode"); var _jwtDecode2 = _interopRequireDefault(_jwtDecode); var _ramda = require("ramda"); var _ramda2 = _interopRequireDefault(_ramda); var _moment = require("moment"); var _moment2 = _interopRequireDefault(_moment); var _consts = require("./consts"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var jwtDecodeMemo = _ramda2.default.memoize(_jwtDecode2.default); var keyInDate = function keyInDate(key) { return _moment2.default.unix(jwtDecodeMemo(key).exp).isAfter((0, _moment2.default)()); }; var getJWTFromIFrame = function getJWTFromIFrame() { var iFrame = document.getElementById("account-iframe"); var responsePromise = new Promise(function (done) { return ( //handle response from account.codogo.io window.addEventListener("message", function (event) { if (event.origin !== _consts.accountURL) { return; } if (event.data.jwt && keyInDate(event.data.jwt)) { localStorage.setItem(_consts.scapholdAuthTokenStorageKey, event.data.jwt); iFrame.remove(); done(event.data.jwt); } else { window.location = _consts.accountURL + "/app/login?come_from=" + window.location; } }, false) ); }); try { iFrame.contentWindow.postMessage("REQUEST_SCAPHOLD_JWT", _consts.accountURL); } catch (e) { //get auth details, tempoary, move to shell at some point iFrame = document.createElement("iframe"); iFrame.src = _consts.accountURL; iFrame.style.display = "none"; iFrame.onload = function () { iFrame.contentWindow.postMessage("REQUEST_SCAPHOLD_JWT", _consts.accountURL); }; document.body.appendChild(iFrame); } return responsePromise; }; //if key is in localstorage and is valid, return the key //if it is not, try to get it from and iframe to account.codogo.io //if the key got from the iframe still isn't valid, redirect to account.codogo.io to refresh, then come back here and try again exports.default = function () { return Promise.resolve(localStorage.getItem(_consts.scapholdAuthTokenStorageKey)).then(function (key) { return key && keyInDate(key) ? key : getJWTFromIFrame(); }); }; //# sourceMappingURL=getJWT.js.map