Skip to content

Commit bfc42d0

Browse files
fix status for connected and authorized
1 parent 9690d30 commit bfc42d0

File tree

19 files changed

+72
-51
lines changed

19 files changed

+72
-51
lines changed

packages/modal/src/modalManager.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import {
88
type BaseConnectorConfig,
99
type ChainNamespaceType,
1010
cloneDeep,
11+
CONNECTED_STATUSES,
1112
CONNECTOR_CATEGORY,
1213
CONNECTOR_EVENTS,
14+
CONNECTOR_INITIAL_AUTHENTICATION_MODE,
1315
CONNECTOR_NAMES,
1416
CONNECTOR_NAMESPACES,
1517
CONNECTOR_STATUS,
@@ -176,7 +178,7 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
176178
public async connect(): Promise<IProvider | null> {
177179
if (!this.loginModal) throw WalletInitializationError.notReady("Login modal is not initialized");
178180
// if already connected return provider
179-
if (this.connectedConnectorName && this.status === CONNECTOR_STATUS.CONNECTED && this.provider) return this.provider;
181+
if (this.connectedConnectorName && CONNECTED_STATUSES.includes(this.status) && this.provider) return this.provider;
180182
this.loginModal.open();
181183
return new Promise((resolve, reject) => {
182184
// remove all listeners when promise is resolved or rejected.
@@ -202,7 +204,12 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
202204
}
203205
};
204206

205-
this.once(CONNECTOR_EVENTS.CONNECTED, handleConnected);
207+
if (this.coreOptions.initialAuthenticationMode === CONNECTOR_INITIAL_AUTHENTICATION_MODE.CONNECT_AND_SIGN) {
208+
this.once(CONNECTOR_EVENTS.AUTHORIZED, handleConnected);
209+
} else {
210+
this.once(CONNECTOR_EVENTS.CONNECTED, handleConnected);
211+
}
212+
206213
this.once(CONNECTOR_EVENTS.ERRORED, handleError);
207214
this.once(LOGIN_MODAL_EVENTS.MODAL_VISIBILITY, handleVisibility);
208215
});
@@ -651,7 +658,7 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
651658
}
652659
if (
653660
!visibility &&
654-
this.status === CONNECTOR_STATUS.CONNECTED &&
661+
CONNECTED_STATUSES.includes(this.status) &&
655662
(walletConnectStatus === CONNECTOR_STATUS.READY || walletConnectStatus === CONNECTOR_STATUS.CONNECTING)
656663
) {
657664
log.debug("this stops wc connector from trying to reconnect once proposal expires");

packages/modal/src/react/context/WalletServicesInnerContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CONNECTOR_STATUS, EVM_PLUGINS, PLUGIN_EVENTS } from "@web3auth/no-modal";
1+
import { CONNECTED_STATUSES, EVM_PLUGINS, PLUGIN_EVENTS } from "@web3auth/no-modal";
22
import { type WalletServicesPluginType } from "@web3auth/no-modal";
33
import { Context, createContext, createElement, PropsWithChildren, useContext, useEffect, useMemo, useState } from "react";
44

@@ -26,7 +26,7 @@ export function WalletServicesContextProvider({ children, context }: PropsWithCh
2626
const plugin = getPlugin(EVM_PLUGINS.WALLET_SERVICES) as WalletServicesPluginType;
2727
setWalletServicesPlugin(plugin);
2828
// when rehydrating, the connectedListener may be registered after the connected event is emitted, we need to check the status here
29-
if (plugin?.status === CONNECTOR_STATUS.CONNECTED) setReady(true);
29+
if (CONNECTED_STATUSES.includes(plugin?.status)) setReady(true);
3030
}
3131
}, [isConnected, getPlugin, walletServicesPlugin]);
3232

packages/modal/src/ui/components/Loader/Loader.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,28 @@ function Loader(props: LoaderProps) {
144144
// eslint-disable-next-line no-console
145145
console.log("connectorName", connectorName);
146146

147+
const isConnectedAccordingToAuthenticationMode = useMemo(
148+
() =>
149+
(!isConnectAndSignAuthenticationMode && modalStatus === MODAL_STATUS.CONNECTED) ||
150+
(isConnectAndSignAuthenticationMode && modalStatus === MODAL_STATUS.AUTHORIZED),
151+
[modalStatus, isConnectAndSignAuthenticationMode]
152+
);
153+
147154
useEffect(() => {
148-
if (modalStatus === MODAL_STATUS.CONNECTED) {
149-
setTimeout(() => {
150-
onClose();
151-
}, 1000);
152-
}
153-
if (isConnectAndSignAuthenticationMode && modalStatus === MODAL_STATUS.AUTHORIZED) {
154-
setTimeout(() => {
155+
if (isConnectedAccordingToAuthenticationMode) {
156+
const timeout = setTimeout(() => {
155157
onClose();
156158
}, 1000);
159+
160+
return () => clearTimeout(timeout);
157161
}
158-
}, [modalStatus, onClose, isConnectAndSignAuthenticationMode]);
162+
}, [isConnectedAccordingToAuthenticationMode, onClose]);
159163

160164
return (
161165
<div className="w3a--flex w3a--h-full w3a--flex-1 w3a--flex-col w3a--items-center w3a--justify-center w3a--gap-y-4">
162166
{modalStatus === MODAL_STATUS.CONNECTING && <ConnectingStatus connector={connector} connectorName={connectorName} appLogo={appLogo} />}
163167

164-
{(modalStatus === MODAL_STATUS.CONNECTED || modalStatus === MODAL_STATUS.AUTHORIZED) && <ConnectedStatus message={message} />}
168+
{isConnectedAccordingToAuthenticationMode && <ConnectedStatus message={message} />}
165169

166170
{modalStatus === MODAL_STATUS.ERRORED && <ErroredStatus message={message} />}
167171

packages/modal/src/ui/components/Widget/Widget.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function Widget(props: WidgetProps) {
170170
};
171171

172172
const onCloseLoader = () => {
173-
if (modalState.status === MODAL_STATUS.CONNECTED) {
173+
if (!isConnectAndSignAuthenticationMode && modalState.status === MODAL_STATUS.CONNECTED) {
174174
setModalState({
175175
...modalState,
176176
modalVisibility: false,
@@ -195,7 +195,10 @@ function Widget(props: WidgetProps) {
195195

196196
const showCloseIcon = useMemo(() => {
197197
return (
198-
modalState.status === MODAL_STATUS.INITIALIZED || modalState.status === MODAL_STATUS.CONNECTED || modalState.status === MODAL_STATUS.ERRORED
198+
modalState.status === MODAL_STATUS.INITIALIZED ||
199+
modalState.status === MODAL_STATUS.CONNECTED ||
200+
modalState.status === MODAL_STATUS.ERRORED ||
201+
modalState.status === MODAL_STATUS.AUTHORIZED
199202
);
200203
}, [modalState.status]);
201204

packages/no-modal/src/base/connector/baseConnector.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SafeEventEmitter } from "@web3auth/auth";
33
import { CHAIN_NAMESPACES, CONNECTOR_NAMESPACES, ConnectorNamespaceType, CustomChainConfig } from "../chain/IChainInterface";
44
import { WalletInitializationError, WalletLoginError } from "../errors";
55
import { WALLET_CONNECTOR_TYPE, WALLET_CONNECTORS } from "../wallet";
6+
import { CONNECTED_STATUSES } from "./connectorStatus";
67
import { CONNECTOR_EVENTS, CONNECTOR_STATUS } from "./constants";
78
import type {
89
BaseConnectorLoginParams,
@@ -41,8 +42,12 @@ export abstract class BaseConnector<T> extends SafeEventEmitter<ConnectorEvents>
4142
this.coreOptions = options.coreOptions;
4243
}
4344

44-
get connnected(): boolean {
45-
return this.status === CONNECTOR_STATUS.CONNECTED;
45+
get connected(): boolean {
46+
return CONNECTED_STATUSES.includes(this.status);
47+
}
48+
49+
get canAuthorize(): boolean {
50+
return ([CONNECTOR_STATUS.AUTHORIZING, CONNECTOR_STATUS.AUTHORIZED, CONNECTOR_STATUS.CONNECTED] as CONNECTOR_STATUS_TYPE[]).includes(this.status);
4651
}
4752

4853
public abstract get provider(): IProvider | null;
@@ -53,7 +58,7 @@ export abstract class BaseConnector<T> extends SafeEventEmitter<ConnectorEvents>
5358
if (this.name === WALLET_CONNECTORS.METAMASK && !this.isInjected && this.status === CONNECTOR_STATUS.CONNECTING) return;
5459

5560
if (this.status === CONNECTOR_STATUS.CONNECTING) throw WalletInitializationError.notReady("Already connecting");
56-
if (this.status === CONNECTOR_STATUS.CONNECTED) throw WalletLoginError.connectionError("Already connected");
61+
if (this.connected) throw WalletLoginError.connectionError("Already connected");
5762
if (this.status !== CONNECTOR_STATUS.READY)
5863
throw WalletLoginError.connectionError(
5964
"Wallet connector is not ready yet, Please wait for init function to resolve before calling connect/connectTo function"
@@ -72,12 +77,12 @@ export abstract class BaseConnector<T> extends SafeEventEmitter<ConnectorEvents>
7277
if (this.connectorNamespace !== CONNECTOR_NAMESPACES.MULTICHAIN && this.connectorNamespace !== chainConfig.chainNamespace)
7378
throw WalletInitializationError.invalidParams("Connector doesn't support this chain namespace");
7479
if (this.status === CONNECTOR_STATUS.NOT_READY) return;
75-
if (this.status === CONNECTOR_STATUS.CONNECTED) throw WalletInitializationError.notReady("Already connected");
80+
if (this.connected) throw WalletInitializationError.notReady("Already connected");
7681
if (this.status === CONNECTOR_STATUS.READY) throw WalletInitializationError.notReady("Connector is already initialized");
7782
}
7883

7984
checkDisconnectionRequirements(): void {
80-
if (this.status !== CONNECTOR_STATUS.CONNECTED) throw WalletLoginError.disconnectionError("Not connected with wallet");
85+
if (!this.connected) throw WalletLoginError.disconnectionError("Not connected with wallet");
8186
}
8287

8388
checkSwitchChainRequirements(params: { chainId: string }, init = false): void {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { CONNECTOR_STATUS } from "./constants";
2+
import { CONNECTOR_STATUS_TYPE } from "./interfaces";
3+
4+
export const CONNECTED_STATUSES: CONNECTOR_STATUS_TYPE[] = [CONNECTOR_STATUS.CONNECTED, CONNECTOR_STATUS.AUTHORIZED];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./baseConnector";
2+
export * from "./connectorStatus";
23
export * from "./constants";
34
export * from "./interfaces";
45
export * from "./utils";

packages/no-modal/src/base/connector/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export interface IConnector<T> extends SafeEventEmitter {
7373
status: CONNECTOR_STATUS_TYPE;
7474
provider: IProvider | null;
7575
connectorData?: unknown;
76-
connnected: boolean;
76+
connected: boolean;
7777
isInjected?: boolean;
7878
icon?: string;
7979
init(options?: ConnectorInitOptions): Promise<void>;

packages/no-modal/src/connectors/auth-connector/authConnector.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
221221
}
222222

223223
public async enableMFA(params: AuthLoginParams = { authConnection: "" }): Promise<void> {
224-
if (this.status !== CONNECTOR_STATUS.CONNECTED) throw WalletLoginError.notConnectedError("Not connected with wallet");
224+
if (!this.connected) throw WalletLoginError.notConnectedError("Not connected with wallet");
225225
if (!this.authInstance) throw WalletInitializationError.notReady("authInstance is not ready");
226226
try {
227227
const result = await this.authInstance.enableMFA(params);
@@ -237,7 +237,7 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
237237
}
238238

239239
public async manageMFA(params: AuthLoginParams = { authConnection: "" }): Promise<void> {
240-
if (this.status !== CONNECTOR_STATUS.CONNECTED) throw WalletLoginError.notConnectedError("Not connected with wallet");
240+
if (!this.connected) throw WalletLoginError.notConnectedError("Not connected with wallet");
241241
if (!this.authInstance) throw WalletInitializationError.notReady("authInstance is not ready");
242242
try {
243243
await this.authInstance.manageMFA(params);
@@ -251,7 +251,7 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
251251
}
252252

253253
async disconnect(options: { cleanup: boolean } = { cleanup: false }): Promise<void> {
254-
if (this.status !== CONNECTOR_STATUS.CONNECTED) throw WalletLoginError.notConnectedError("Not connected with wallet");
254+
if (!this.connected) throw WalletLoginError.notConnectedError("Not connected with wallet");
255255
if (!this.authInstance) throw WalletInitializationError.notReady("authInstance is not ready");
256256
this.status = CONNECTOR_STATUS.DISCONNECTING;
257257
await this.authInstance.logout();
@@ -271,13 +271,13 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
271271
}
272272

273273
async getIdentityToken(): Promise<{ idToken: string }> {
274-
if (this.status !== CONNECTOR_STATUS.CONNECTED) throw WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first");
274+
if (!this.canAuthorize) throw WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first");
275275
const userInfo = await this.getUserInfo();
276276
return { idToken: userInfo.idToken as string };
277277
}
278278

279279
async getUserInfo(): Promise<Partial<UserInfo>> {
280-
if (this.status !== CONNECTOR_STATUS.CONNECTED) throw WalletLoginError.notConnectedError("Not connected with wallet");
280+
if (!this.canAuthorize) throw WalletLoginError.notConnectedError("Not connected with wallet");
281281
if (!this.authInstance) throw WalletInitializationError.notReady("authInstance is not ready");
282282
const userInfo = this.authInstance.getUserInfo();
283283
return userInfo;

packages/no-modal/src/connectors/base-evm-connector/baseEvmConnector.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import {
66
checkIfTokenIsExpired,
77
clearToken,
88
CONNECTOR_EVENTS,
9-
CONNECTOR_STATUS,
10-
CONNECTOR_STATUS_TYPE,
119
ConnectorInitOptions,
1210
getSavedToken,
1311
IdentityTokenInfo,
@@ -20,8 +18,7 @@ export abstract class BaseEvmConnector<T> extends BaseConnector<T> {
2018
async init(_?: ConnectorInitOptions): Promise<void> {}
2119

2220
async getIdentityToken(): Promise<IdentityTokenInfo> {
23-
if (!this.provider || !([CONNECTOR_STATUS.CONNECTED, CONNECTOR_STATUS.AUTHORIZING] as CONNECTOR_STATUS_TYPE[]).includes(this.status))
24-
throw WalletLoginError.notConnectedError();
21+
if (!this.provider || !this.canAuthorize) throw WalletLoginError.notConnectedError();
2522
if (!this.coreOptions) throw WalletInitializationError.invalidParams("Please initialize Web3Auth with valid options");
2623

2724
const accounts = await this.provider.request<never, string[]>({ method: EVM_METHOD_TYPES.GET_ACCOUNTS });

0 commit comments

Comments
 (0)