({ network, clientId }: { network: WEB3AUTH_NETWORK_TYPE; clientId: string })
| 85 | } |
| 86 | |
| 87 | async init({ network, clientId }: { network: WEB3AUTH_NETWORK_TYPE; clientId: string }): Promise<void> { |
| 88 | if (typeof window === "undefined" || typeof document === "undefined") throw new Error("window or document is not available"); |
| 89 | if (this.initialized) throw new Error("AuthProvider already initialized"); |
| 90 | |
| 91 | const authIframeUrl = new URL(this.sdkUrl); |
| 92 | if (authIframeUrl.pathname.endsWith("/")) authIframeUrl.pathname += "frame"; |
| 93 | else authIframeUrl.pathname += "/frame"; |
| 94 | |
| 95 | const hashParams = new URLSearchParams(); |
| 96 | hashParams.append("origin", window.location.origin); |
| 97 | hashParams.append("nonce", this.embedNonce); |
| 98 | authIframeUrl.hash = hashParams.toString(); |
| 99 | |
| 100 | const colorScheme = getTheme(this.whiteLabel.mode || THEME_MODES.light); |
| 101 | |
| 102 | const authServiceIframe = htmlToElement<HTMLIFrameElement>( |
| 103 | `<iframe |
| 104 | id="${IFRAME_MODAL_ID}-${this.embedNonce}" |
| 105 | class="${IFRAME_MODAL_ID}-${this.embedNonce}" |
| 106 | sandbox="allow-popups allow-scripts allow-same-origin allow-forms allow-modals allow-downloads" |
| 107 | src="${authIframeUrl.href}" |
| 108 | style="display: none; position: fixed; top: 0; right: 0; width: 100%; z-index: 10000000; |
| 109 | height: 100%; border: none; border-radius: 0; color-scheme: ${colorScheme};" |
| 110 | allow="clipboard-write" |
| 111 | ></iframe>` |
| 112 | ); |
| 113 | |
| 114 | this.registerAuthServiceIframe(authServiceIframe); |
| 115 | |
| 116 | return new Promise<void>((resolve, reject) => { |
| 117 | try { |
| 118 | window.document.body.appendChild(authServiceIframe); |
| 119 | |
| 120 | this.messageHandler = (event: MessageEvent) => { |
| 121 | if (event.origin !== this.targetOrigin) return; |
| 122 | const { data } = event as { |
| 123 | data: { type: string; nonce: string; data: AuthFlowResult }; |
| 124 | }; |
| 125 | const { type, nonce } = data; |
| 126 | // dont do anything if the nonce is not the same. |
| 127 | if (nonce !== this.embedNonce) return; |
| 128 | const messageData = data.data; |
| 129 | switch (type) { |
| 130 | case JRPC_METHODS.SETUP_COMPLETE: |
| 131 | this.getAuthServiceIframe()?.contentWindow?.postMessage( |
| 132 | { |
| 133 | type: JRPC_METHODS.INIT_DAPP, |
| 134 | data: { |
| 135 | network, |
| 136 | clientId, |
| 137 | }, |
| 138 | }, |
| 139 | this.targetOrigin |
| 140 | ); |
| 141 | this.initialized = true; |
| 142 | resolve(); |
| 143 | break; |
| 144 | case JRPC_METHODS.LOGIN_FAILED: |
nothing calls this directly
no test coverage detected