@authsignal/browser 0.0.1 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -3534,40 +3534,48 @@ if (typeof document !== 'undefined') {
3534
3534
  }
3535
3535
  }
3536
3536
 
3537
- var DIALOG_CONTAINER_ID = "authsignal-popup";
3538
- var DIALOG_CONTENT_ID = "authsignal-popup-content";
3537
+ var CONTAINER_ID = "__authsignal-popup-container";
3538
+ var CONTENT_ID = "__authsignal-popup-content";
3539
+ var OVERLAY_ID = "__authsignal-popup-overlay";
3540
+ var STYLE_ID = "__authsignal-popup-style";
3539
3541
  var PopupHandler = /** @class */ (function () {
3540
3542
  function PopupHandler() {
3541
3543
  this.popup = null;
3542
- if (document.querySelector("#".concat(DIALOG_CONTAINER_ID))) {
3544
+ if (document.querySelector("#".concat(CONTAINER_ID))) {
3543
3545
  throw new Error("Multiple instances of Authsignal popup is not supported.");
3544
3546
  }
3545
3547
  this.create();
3546
3548
  }
3547
3549
  PopupHandler.prototype.create = function () {
3548
- // Dialog container
3550
+ // Create dialog container
3549
3551
  var container = document.createElement("div");
3550
- container.setAttribute("id", DIALOG_CONTAINER_ID);
3552
+ container.setAttribute("id", CONTAINER_ID);
3551
3553
  container.setAttribute("aria-hidden", "true");
3552
- container.setAttribute("class", "dialog-container");
3553
- // Dialog overlay
3554
+ // Create dialog overlay
3554
3555
  var overlay = document.createElement("div");
3555
- overlay.setAttribute("class", "dialog-overlay");
3556
+ overlay.setAttribute("id", OVERLAY_ID);
3556
3557
  overlay.setAttribute("data-a11y-dialog-hide", "true");
3557
- container.appendChild(overlay);
3558
- // Dialog content
3558
+ // Create dialog content
3559
3559
  var content = document.createElement("div");
3560
- content.setAttribute("class", "dialog-content");
3561
- content.setAttribute("id", DIALOG_CONTENT_ID);
3562
- container.appendChild(content);
3560
+ content.setAttribute("id", CONTENT_ID);
3563
3561
  document.body.appendChild(container);
3562
+ // Create CSS for dialog
3563
+ var style = document.createElement("style");
3564
+ style.setAttribute("id", STYLE_ID);
3565
+ style.textContent = "\n #".concat(CONTAINER_ID, ",\n #").concat(OVERLAY_ID, " {\n position: fixed;\n top: 0;\n right: 0;\n bottom: 0;\n left: 0;\n }\n\n #").concat(CONTAINER_ID, " {\n z-index: 2;\n display: flex;\n }\n\n #").concat(CONTAINER_ID, "[aria-hidden='true'] {\n display: none;\n }\n\n #").concat(OVERLAY_ID, " {\n background-color: rgba(43, 46, 56, 0.9);\n }\n\n #").concat(CONTENT_ID, " {\n margin: auto;\n z-index: 2;\n position: relative;\n background-color: white;\n height: 600px;\n width: 600px;\n border-radius: 5px;\n }\n\n #").concat(CONTENT_ID, " iframe {\n width: 100%;\n height: 100%;\n }\n ");
3566
+ // Attach the created elements
3567
+ document.head.insertAdjacentElement("beforeend", style);
3568
+ container.appendChild(overlay);
3569
+ container.appendChild(content);
3564
3570
  this.popup = new A11yDialog(container);
3565
3571
  this.popup.on("hide", this.destroy);
3566
3572
  };
3567
3573
  PopupHandler.prototype.destroy = function () {
3568
- var dialogEl = document.querySelector("#".concat(DIALOG_CONTAINER_ID));
3569
- if (dialogEl) {
3574
+ var dialogEl = document.querySelector("#".concat(CONTAINER_ID));
3575
+ var styleEl = document.querySelector("#".concat(STYLE_ID));
3576
+ if (dialogEl && styleEl) {
3570
3577
  document.body.removeChild(dialogEl);
3578
+ document.head.removeChild(styleEl);
3571
3579
  }
3572
3580
  };
3573
3581
  PopupHandler.prototype.show = function (_a) {
@@ -3579,10 +3587,8 @@ var PopupHandler = /** @class */ (function () {
3579
3587
  iframe.setAttribute("name", "authsignal");
3580
3588
  iframe.setAttribute("title", "Authsignal multi-factor authentication challenge");
3581
3589
  iframe.setAttribute("src", challengeUrl);
3582
- iframe.setAttribute("width", "600");
3583
- iframe.setAttribute("height", "600");
3584
3590
  iframe.setAttribute("frameborder", "0");
3585
- var dialogContent = document.querySelector("#".concat(DIALOG_CONTENT_ID));
3591
+ var dialogContent = document.querySelector("#".concat(CONTENT_ID));
3586
3592
  if (dialogContent) {
3587
3593
  dialogContent.appendChild(iframe);
3588
3594
  }
@@ -3597,29 +3603,28 @@ var PopupHandler = /** @class */ (function () {
3597
3603
  return PopupHandler;
3598
3604
  }());
3599
3605
 
3600
- function authsignalClient(publishableKey, options) {
3601
- var client = new AuthsignalClient();
3606
+ function authsignalBrowser(publishableKey, options) {
3607
+ var client = new AuthsignalBrowser();
3602
3608
  client.init(publishableKey, options);
3603
3609
  return client;
3604
3610
  }
3605
- var AuthsignalClient = /** @class */ (function () {
3606
- function AuthsignalClient() {
3611
+ var AuthsignalBrowser = /** @class */ (function () {
3612
+ function AuthsignalBrowser() {
3607
3613
  this.anonymousId = "";
3608
- this.initialized = false;
3609
3614
  this.publishableKey = "";
3610
3615
  this.cookieDomain = "";
3611
3616
  this.idCookieName = "";
3612
3617
  this.trackingHost = "";
3613
3618
  }
3614
- AuthsignalClient.prototype.init = function (publishableKey, options) {
3619
+ AuthsignalBrowser.prototype.init = function (publishableKey, options) {
3615
3620
  return __awaiter(this, void 0, void 0, function () {
3616
3621
  var _a, anonymousId, isGeneratedAnonymousId, agent, registerAnonymousIdRequest;
3617
3622
  return __generator(this, function (_b) {
3618
3623
  switch (_b.label) {
3619
3624
  case 0:
3620
- this.cookieDomain = (options === null || options === void 0 ? void 0 : options.cookie_domain) || getCookieDomain();
3621
- this.idCookieName = (options === null || options === void 0 ? void 0 : options.cookie_name) || "__as_aid";
3622
- this.trackingHost = getHostWithProtocol((options === null || options === void 0 ? void 0 : options.tracking_host) || "t.authsignal.com");
3625
+ this.cookieDomain = (options === null || options === void 0 ? void 0 : options.cookieDomain) || getCookieDomain();
3626
+ this.idCookieName = (options === null || options === void 0 ? void 0 : options.cookieName) || "__as_aid";
3627
+ this.trackingHost = getHostWithProtocol((options === null || options === void 0 ? void 0 : options.trackingHost) || "t.authsignal.com");
3623
3628
  _a = this;
3624
3629
  return [4 /*yield*/, load({
3625
3630
  monitoring: false
@@ -3643,14 +3648,12 @@ var AuthsignalClient = /** @class */ (function () {
3643
3648
  case 3:
3644
3649
  _b.sent();
3645
3650
  _b.label = 4;
3646
- case 4:
3647
- this.initialized = true;
3648
- return [2 /*return*/];
3651
+ case 4: return [2 /*return*/];
3649
3652
  }
3650
3653
  });
3651
3654
  });
3652
3655
  };
3653
- AuthsignalClient.prototype.identify = function (props) {
3656
+ AuthsignalBrowser.prototype.identify = function (props) {
3654
3657
  return __awaiter(this, void 0, void 0, function () {
3655
3658
  var request;
3656
3659
  return __generator(this, function (_a) {
@@ -3664,7 +3667,7 @@ var AuthsignalClient = /** @class */ (function () {
3664
3667
  });
3665
3668
  });
3666
3669
  };
3667
- AuthsignalClient.prototype.getAnonymousId = function () {
3670
+ AuthsignalBrowser.prototype.getAnonymousId = function () {
3668
3671
  var idCookie = getCookie(this.idCookieName);
3669
3672
  if (idCookie) {
3670
3673
  return { idCookie: idCookie, generated: false };
@@ -3673,25 +3676,34 @@ var AuthsignalClient = /** @class */ (function () {
3673
3676
  setCookie("__as_aid", newId, Infinity, this.cookieDomain, document.location.protocol !== "http:");
3674
3677
  return { idCookie: newId, generated: true };
3675
3678
  };
3676
- AuthsignalClient.prototype.challengeWithPopup = function (_a) {
3677
- var challengeUrl = _a.challengeUrl;
3678
- var Popup = new PopupHandler();
3679
- Popup.show({ challengeUrl: challengeUrl });
3680
- return new Promise(function (resolve, reject) {
3681
- var handleChallenge = function (event) {
3682
- if (event.data === "authsignal-challenge-success") {
3683
- Popup.close();
3684
- resolve(true);
3685
- }
3686
- else if (event.data === "authsignal-challenge-failure") {
3687
- Popup.close();
3688
- reject(false);
3689
- }
3690
- };
3691
- window.addEventListener("message", handleChallenge, false);
3692
- });
3679
+ AuthsignalBrowser.prototype.mfa = function (_a) {
3680
+ var url = _a.url;
3681
+ window.location.href = url;
3682
+ };
3683
+ AuthsignalBrowser.prototype.challenge = function (_a) {
3684
+ var challengeUrl = _a.challengeUrl, _b = _a.mode, mode = _b === void 0 ? "redirect" : _b;
3685
+ if (mode === "redirect") {
3686
+ window.location.href = challengeUrl;
3687
+ }
3688
+ else {
3689
+ var Popup_1 = new PopupHandler();
3690
+ Popup_1.show({ challengeUrl: challengeUrl });
3691
+ return new Promise(function (resolve, reject) {
3692
+ var handleChallenge = function (event) {
3693
+ if (event.data === "authsignal-challenge-success") {
3694
+ Popup_1.close();
3695
+ resolve(true);
3696
+ }
3697
+ else if (event.data === "authsignal-challenge-failure") {
3698
+ Popup_1.close();
3699
+ reject(false);
3700
+ }
3701
+ };
3702
+ window.addEventListener("message", handleChallenge, false);
3703
+ });
3704
+ }
3693
3705
  };
3694
- AuthsignalClient.prototype.registerIdentity = function (request) {
3706
+ AuthsignalBrowser.prototype.registerIdentity = function (request) {
3695
3707
  return __awaiter(this, void 0, void 0, function () {
3696
3708
  var result;
3697
3709
  return __generator(this, function (_a) {
@@ -3706,7 +3718,7 @@ var AuthsignalClient = /** @class */ (function () {
3706
3718
  });
3707
3719
  });
3708
3720
  };
3709
- AuthsignalClient.prototype.registerAnonymousId = function (request) {
3721
+ AuthsignalBrowser.prototype.registerAnonymousId = function (request) {
3710
3722
  return __awaiter(this, void 0, void 0, function () {
3711
3723
  var result;
3712
3724
  return __generator(this, function (_a) {
@@ -3721,7 +3733,7 @@ var AuthsignalClient = /** @class */ (function () {
3721
3733
  });
3722
3734
  });
3723
3735
  };
3724
- AuthsignalClient.prototype.buildRegisterAnonymousIdRequest = function () {
3736
+ AuthsignalBrowser.prototype.buildRegisterAnonymousIdRequest = function () {
3725
3737
  var now = new Date();
3726
3738
  return {
3727
3739
  deviceFingerprint: this.deviceFingerprint || "",
@@ -3740,14 +3752,14 @@ var AuthsignalClient = /** @class */ (function () {
3740
3752
  }
3741
3753
  };
3742
3754
  };
3743
- AuthsignalClient.prototype.sendJson = function (path, payload) {
3755
+ AuthsignalBrowser.prototype.sendJson = function (path, payload) {
3744
3756
  var jsonString = JSON.stringify(payload);
3745
3757
  var url = "".concat(this.trackingHost, "/api/v1/client/").concat(path, "?publishableKey=").concat(this.publishableKey);
3746
3758
  // Think about encapsulating the payloads around a message contract
3747
3759
  // SDK client versions, additional meta data
3748
3760
  return this.xmlHttpReqTransport(url, jsonString);
3749
3761
  };
3750
- AuthsignalClient.prototype.xmlHttpReqTransport = function (url, json) {
3762
+ AuthsignalBrowser.prototype.xmlHttpReqTransport = function (url, json) {
3751
3763
  var req = new XMLHttpRequest();
3752
3764
  return new Promise(function (resolve, reject) {
3753
3765
  req.onerror = function () {
@@ -3764,8 +3776,8 @@ var AuthsignalClient = /** @class */ (function () {
3764
3776
  req.send(json);
3765
3777
  });
3766
3778
  };
3767
- return AuthsignalClient;
3779
+ return AuthsignalBrowser;
3768
3780
  }());
3769
3781
 
3770
- export { AuthsignalClient, authsignalClient };
3782
+ export { AuthsignalBrowser, authsignalBrowser };
3771
3783
  //# sourceMappingURL=index.js.map