Skip to content

Commit e6464a2

Browse files
committed
adds cleanup
1 parent dd97a52 commit e6464a2

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

src/core/AuthProvider.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,7 @@ export async function preloadIframe() {
2323
}
2424
}
2525

26-
const authServiceIframeMap: WeakMap<object, HTMLIFrameElement> = new WeakMap();
27-
28-
const iframeCleanupRegistry = new FinalizationRegistry((heldValue: HTMLIFrameElement) => {
29-
log.info("Cleaning up iframe", heldValue);
30-
31-
// Remove iframe from DOM
32-
if (heldValue && heldValue.parentNode) {
33-
heldValue.parentNode.removeChild(heldValue);
34-
}
35-
});
26+
const authServiceIframeMap: Map<string, HTMLIFrameElement> = new Map();
3627

3728
function getTheme(theme: THEME_MODE_TYPE): string {
3829
if (theme === THEME_MODES.light) return "light";
@@ -50,7 +41,7 @@ export class AuthProvider {
5041

5142
private loginCallbackFailed: ((reason?: string) => void) | null = null;
5243

53-
private readonly embedNonce = { id: randomId() };
44+
private readonly embedNonce = randomId();
5445

5546
constructor({ sdkUrl, whiteLabel }: { sdkUrl: string; whiteLabel: WhiteLabelData }) {
5647
this.sdkUrl = sdkUrl;
@@ -62,12 +53,19 @@ export class AuthProvider {
6253
}
6354

6455
getAuthServiceIframe(): HTMLIFrameElement {
65-
return authServiceIframeMap.get(this) as HTMLIFrameElement;
56+
return authServiceIframeMap.get(this.embedNonce) as HTMLIFrameElement;
6657
}
6758

6859
registerAuthServiceIframe(iframe: HTMLIFrameElement) {
69-
authServiceIframeMap.set(this, iframe);
70-
iframeCleanupRegistry.register(this, iframe);
60+
authServiceIframeMap.set(this.embedNonce, iframe);
61+
}
62+
63+
public cleanup() {
64+
const iframe = authServiceIframeMap.get(this.embedNonce);
65+
if (iframe && iframe.parentNode) {
66+
iframe.parentNode.removeChild(iframe);
67+
authServiceIframeMap.delete(this.embedNonce);
68+
}
7169
}
7270

7371
async init({ network, clientId }: { network: WEB3AUTH_NETWORK_TYPE; clientId: string }): Promise<void> {
@@ -80,15 +78,15 @@ export class AuthProvider {
8078

8179
const hashParams = new URLSearchParams();
8280
hashParams.append("origin", window.location.origin);
83-
hashParams.append("nonce", this.embedNonce.id);
81+
hashParams.append("nonce", this.embedNonce);
8482
authIframeUrl.hash = hashParams.toString();
8583

8684
const colorScheme = getTheme(this.whiteLabel.mode || THEME_MODES.light);
8785

8886
const authServiceIframe = htmlToElement<HTMLIFrameElement>(
8987
`<iframe
90-
id="${IFRAME_MODAL_ID}-${this.embedNonce.id}"
91-
class="${IFRAME_MODAL_ID}-${this.embedNonce.id}"
88+
id="${IFRAME_MODAL_ID}-${this.embedNonce}"
89+
class="${IFRAME_MODAL_ID}-${this.embedNonce}"
9290
sandbox="allow-popups allow-scripts allow-same-origin allow-forms allow-modals allow-downloads"
9391
src="${authIframeUrl.href}"
9492
style="display: none; position: fixed; top: 0; right: 0; width: 100%; z-index: 10000000;
@@ -110,7 +108,7 @@ export class AuthProvider {
110108
};
111109
const { type, nonce } = data;
112110
// dont do anything if the nonce is not the same.
113-
if (nonce !== this.embedNonce.id) return;
111+
if (nonce !== this.embedNonce) return;
114112
const messageData = data.data;
115113
switch (type) {
116114
case JRPC_METHODS.SETUP_COMPLETE:

src/core/auth.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,10 @@ export class Auth {
433433
return true;
434434
}
435435

436+
async cleanup() {
437+
if (this.authProvider) this.authProvider.cleanup();
438+
}
439+
436440
getUserInfo(): AuthUserInfo {
437441
if (!this.sessionManager.sessionId) {
438442
throw LoginError.userNotLoggedIn();

0 commit comments

Comments
 (0)