Skip to content

Commit 9c917b6

Browse files
committed
resolved comments
1 parent 6d7bfa6 commit 9c917b6

4 files changed

Lines changed: 21 additions & 13 deletions

File tree

src/core/AuthProvider.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export class AuthProvider {
5050

5151
private loginCallbackFailed: ((reason?: string) => void) | null = null;
5252

53+
private messageHandler: ((event: MessageEvent) => void) | null = null;
54+
5355
private readonly embedNonce = randomId();
5456

5557
constructor({ sdkUrl, whiteLabel }: { sdkUrl: string; whiteLabel: WhiteLabelData }) {
@@ -70,11 +72,16 @@ export class AuthProvider {
7072
}
7173

7274
public cleanup() {
75+
if (this.messageHandler) {
76+
window.removeEventListener("message", this.messageHandler);
77+
this.messageHandler = null;
78+
}
7379
const iframe = authServiceIframeMap.get(this.embedNonce);
7480
if (iframe && iframe.parentNode) {
7581
iframe.parentNode.removeChild(iframe);
7682
authServiceIframeMap.delete(this.embedNonce);
7783
}
84+
this.initialized = false;
7885
}
7986

8087
async init({ network, clientId }: { network: WEB3AUTH_NETWORK_TYPE; clientId: string }): Promise<void> {
@@ -110,7 +117,7 @@ export class AuthProvider {
110117
try {
111118
window.document.body.appendChild(authServiceIframe);
112119

113-
const handleMessage = (event: MessageEvent) => {
120+
this.messageHandler = (event: MessageEvent) => {
114121
if (event.origin !== this.targetOrigin) return;
115122
const { data } = event as {
116123
data: { type: string; nonce: string; data: AuthFlowResult };
@@ -154,7 +161,7 @@ export class AuthProvider {
154161
}
155162
};
156163

157-
window.addEventListener("message", handleMessage);
164+
window.addEventListener("message", this.messageHandler);
158165
} catch (error) {
159166
reject(error);
160167
}

src/core/auth.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export class Auth {
123123
}
124124

125125
get appState(): string {
126-
return this.state.userInfo.appState || this.dappState || "";
126+
return this.state?.userInfo?.appState || this.dappState || "";
127127
}
128128

129129
get baseUrl(): string {
@@ -321,7 +321,7 @@ export class Auth {
321321
};
322322

323323
const result = await this.authHandler(`${this.baseUrl}/start`, dataObject, POPUP_TIMEOUT);
324-
if (!result) return null;
324+
if (!result) return false;
325325
if (isAuthFlowError(result)) {
326326
this.dappState = result.state;
327327
throw LoginError.loginFailed(result.error);
@@ -399,7 +399,7 @@ export class Auth {
399399
};
400400

401401
const result = await this.authHandler(`${this.baseUrl}/start`, dataObject);
402-
if (!result) return undefined;
402+
if (!result) return false;
403403
if (isAuthFlowError(result)) return false;
404404
return true;
405405
}
@@ -418,7 +418,7 @@ export class Auth {
418418
};
419419

420420
const result = await this.authHandler(`${this.baseUrl}/start`, dataObject);
421-
if (!result) return undefined;
421+
if (!result) return false;
422422
if (isAuthFlowError(result)) return false;
423423
return true;
424424
}
@@ -437,7 +437,7 @@ export class Auth {
437437
};
438438

439439
const result = await this.authHandler(`${this.baseUrl}/start`, dataObject);
440-
if (!result) return undefined;
440+
if (!result) return false;
441441
if (isAuthFlowError(result)) return false;
442442
return true;
443443
}
@@ -479,7 +479,7 @@ export class Auth {
479479
}
480480
}
481481

482-
private async _authorizeSession(): Promise<AuthSessionData> {
482+
private async _authorizeSession(): Promise<AuthSessionData | null> {
483483
try {
484484
const result = await this.sessionManager.authorize();
485485
return result;
@@ -499,7 +499,7 @@ export class Auth {
499499
this.updateState(result);
500500
}
501501

502-
private async authHandler(url: string, dataObject: AuthRequestPayload, popupTimeout = 1000 * 10): Promise<AuthFlowResult | undefined> {
502+
private async authHandler(url: string, dataObject: AuthRequestPayload, popupTimeout = 1000 * 10): Promise<AuthFlowResult | null> {
503503
const loginId = StorageManager.generateRandomSessionKey();
504504
await this.storeAuthPayload(loginId, dataObject);
505505
const configParams: BaseLoginParams = {
@@ -514,7 +514,7 @@ export class Auth {
514514
hash: { b64Params: jsonToBase64(configParams) },
515515
});
516516
window.location.href = loginUrl;
517-
return undefined;
517+
return null;
518518
}
519519

520520
const loginUrl = constructURL({

src/utils/interfaces.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,8 @@ export type AuthOptions = {
523523
useCoreKitKey?: boolean;
524524

525525
/**
526-
* This parameter will be used to select core kit key.
527-
* @defaultValue false
526+
* This parameter will be used to select sdk mode.
527+
* @defaultValue default
528528
*/
529529
sdkMode?: SDK_MODE_TYPE;
530530

src/utils/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { decodeBase64Url, encodeBase64Url } from "@toruslabs/metadata-helpers";
2+
import { klona } from "klona/json";
23

34
export function safebtoa(str: string): string {
45
return encodeBase64Url(str);
@@ -26,7 +27,7 @@ export const htmlToElement = <T extends Element>(html: string): T => {
2627

2728
export function cloneDeep<T>(object: T): T {
2829
try {
29-
return structuredClone(object);
30+
return klona(object);
3031
} catch {
3132
return JSON.parse(JSON.stringify(object));
3233
}

0 commit comments

Comments
 (0)