Skip to content

Commit

Permalink
[AUT-171] - Add onClose event handler + resolve promise when popup …
Browse files Browse the repository at this point in the history
…closes (#12)

* Resolve promise on popup close

* Bump version

* Remove onClose
  • Loading branch information
hwhmeikle authored Jul 26, 2022
1 parent caee9b1 commit e684b09
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@authsignal/browser",
"type": "module",
"version": "0.0.8",
"version": "0.0.9",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/index.d.ts",
Expand Down
5 changes: 4 additions & 1 deletion src/authsignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,14 @@ export class Authsignal {
if (event.origin === this.endpoint) {
if (event.data === AuthsignalWindowMessage.AUTHSIGNAL_CLOSE_POPUP) {
Popup.close();
resolve(true);
}
}
};

Popup.on("hide", () => {
resolve(true);
});

window.addEventListener("message", onMessage, false);
});
}
Expand Down
21 changes: 20 additions & 1 deletion src/popup-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ type PopupShowInput = {
url: string;
};

type EventType = "show" | "hide" | "destroy" | "create";

type EventHandler = (node: Element, event?: Event) => void;

class PopupHandler {
private popup: A11yDialog | null = null;

Expand Down Expand Up @@ -85,12 +89,17 @@ class PopupHandler {
container.appendChild(content);

this.popup = new A11yDialog(container);
this.popup.on("hide", this.destroy);

// Make sure to remove any trace of the dialog on hide
this.popup.on("hide", () => {
this.destroy();
});
}

destroy() {
const dialogEl = document.querySelector(`#${CONTAINER_ID}`);
const styleEl = document.querySelector(`#${STYLE_ID}`);

if (dialogEl && styleEl) {
document.body.removeChild(dialogEl);
document.head.removeChild(styleEl);
Expand All @@ -103,12 +112,14 @@ class PopupHandler {
}

const iframe = document.createElement("iframe");

iframe.setAttribute("name", "authsignal");
iframe.setAttribute("title", "Authsignal multi-factor authentication");
iframe.setAttribute("src", url);
iframe.setAttribute("frameborder", "0");

const dialogContent = document.querySelector(`#${CONTENT_ID}`);

if (dialogContent) {
dialogContent.appendChild(iframe);
}
Expand All @@ -123,6 +134,14 @@ class PopupHandler {

this.popup.hide();
}

on(event: EventType, handler: EventHandler) {
if (!this.popup) {
throw new Error("Popup is not initialized");
}

this.popup.on(event, handler);
}
}

export {PopupHandler};
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export type MfaInput = {
};

export type LaunchOptions = {
/**
* How the Authsignal Prebuilt MFA page should launch.
* `popup` will cause it to open in a overlay, whilst `redirect`
* will trigger a full page redirect.
* If no value is supplied, mode defaults to `redirect`.
*/
mode?: "popup" | "redirect";
};

Expand Down

0 comments on commit e684b09

Please sign in to comment.