Skip to content

Commit 4d41912

Browse files
Merge pull request #366 from Web3Auth/feat/optimize-iframe-loading
optimize iframe loading
2 parents 01f48c7 + e6464a2 commit 4d41912

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/core/AuthProvider.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ export class AuthProvider {
5656
return authServiceIframeMap.get(this.embedNonce) as HTMLIFrameElement;
5757
}
5858

59+
registerAuthServiceIframe(iframe: HTMLIFrameElement) {
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+
}
69+
}
70+
5971
async init({ network, clientId }: { network: WEB3AUTH_NETWORK_TYPE; clientId: string }): Promise<void> {
6072
if (typeof window === "undefined" || typeof document === "undefined") throw new Error("window or document is not available");
6173
if (this.initialized) throw new Error("AuthProvider already initialized");
@@ -82,7 +94,8 @@ export class AuthProvider {
8294
allow="clipboard-write"
8395
></iframe>`
8496
);
85-
authServiceIframeMap.set(this.embedNonce, authServiceIframe);
97+
98+
this.registerAuthServiceIframe(authServiceIframe);
8699

87100
return new Promise<void>((resolve, reject) => {
88101
try {

src/core/auth.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export class Auth {
5050

5151
private authProvider: AuthProvider;
5252

53+
private authProviderPromise: Promise<void>;
54+
5355
constructor(options: AuthOptions) {
5456
if (!options.clientId) throw InitializationError.invalidParams("clientId is required");
5557
if (!options.network) options.network = WEB3AUTH_NETWORK.SAPPHIRE_MAINNET;
@@ -192,8 +194,9 @@ export class Auth {
192194
if (this.options.sdkMode === SDK_MODE.IFRAME) {
193195
this.authProvider = new AuthProvider({ sdkUrl: this.options.sdkUrl, whiteLabel: this.options.whiteLabel });
194196
if (!this.state.sessionId) {
195-
await this.authProvider.init({ network: this.options.network, clientId: this.options.clientId });
197+
this.authProviderPromise = this.authProvider.init({ network: this.options.network, clientId: this.options.clientId });
196198
if (params.nonce) {
199+
await this.authProviderPromise;
197200
await this.postLoginInitiatedMessage(JSON.parse(params.loginParams), params.nonce);
198201
}
199202
}
@@ -227,8 +230,13 @@ export class Auth {
227230

228231
async postLoginInitiatedMessage(params: LoginParams, nonce?: string): Promise<void> {
229232
if (this.options.sdkMode !== SDK_MODE.IFRAME) throw LoginError.invalidLoginParams("Cannot perform this action in default mode.");
233+
// This is to ensure that the auth provider is initialized before calling postLoginInitiatedMessage
234+
// This is setup in the init method, if there is no active session.
235+
if (this.authProviderPromise) await this.authProviderPromise;
230236

231-
if (!this.authProvider || !this.authProvider.initialized) {
237+
// if there is an active session, we dont load the auth provider in the init method.
238+
// so we need to initialize it here, if user logged out and then login in again.
239+
if (!this.authProvider?.initialized) {
232240
await this.authProvider.init({ network: this.options.network, clientId: this.options.clientId });
233241
}
234242

@@ -242,7 +250,9 @@ export class Auth {
242250

243251
async postLoginCancelledMessage(nonce: string): Promise<void> {
244252
if (this.options.sdkMode !== SDK_MODE.IFRAME) throw LoginError.invalidLoginParams("Cannot perform this action in default mode.");
245-
if (!this.authProvider || !this.authProvider.initialized) throw InitializationError.notInitialized();
253+
if (this.authProviderPromise) await this.authProviderPromise;
254+
255+
if (!this.authProvider?.initialized) throw InitializationError.notInitialized();
246256

247257
this.authProvider.postLoginCancelledMessage(nonce);
248258
}
@@ -423,6 +433,10 @@ export class Auth {
423433
return true;
424434
}
425435

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

0 commit comments

Comments
 (0)