Skip to content

Commit 2563a69

Browse files
fix no-modal
1 parent 8568c46 commit 2563a69

File tree

16 files changed

+106
-78
lines changed

16 files changed

+106
-78
lines changed

packages/modal/src/react/hooks/useWeb3AuthAccount.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import { useCallback, useEffect, useState } from "react";
33

44
import { useWeb3AuthInner } from "./useWeb3AuthInner";
55

6-
export interface IUseWeb3AuthAccount {
6+
export interface IUseWeb3AuthUser {
77
loading: boolean;
88
error: Web3AuthError | null;
99
userInfo: Partial<UserInfo> | null;
1010
isMFAEnabled: boolean;
1111
getUserInfo: () => Promise<Partial<UserInfo> | null>;
1212
}
1313

14-
export const useWeb3AuthAccount = (): IUseWeb3AuthAccount => {
15-
const { web3Auth, isAuthenticated } = useWeb3AuthInner();
14+
export const useWeb3AuthUser = (): IUseWeb3AuthUser => {
15+
const { web3Auth, isConnected } = useWeb3AuthInner();
1616

1717
const [isMFAEnabled, setIsMFAEnabled] = useState(false);
1818
const [userInfo, setUserInfo] = useState<Partial<UserInfo> | null>(null);
@@ -39,12 +39,12 @@ export const useWeb3AuthAccount = (): IUseWeb3AuthAccount => {
3939
setUserInfo(userInfo);
4040
};
4141

42-
if (isAuthenticated && !userInfo) saveUserInfo();
42+
if (isConnected && !userInfo) saveUserInfo();
4343

44-
if (!isAuthenticated && userInfo) {
44+
if (!isConnected && userInfo) {
4545
setUserInfo(null);
4646
}
47-
}, [isAuthenticated, userInfo, getUserInfo]);
47+
}, [isConnected, userInfo, getUserInfo]);
4848

4949
useEffect(() => {
5050
const mfaEnabledListener = async (isMFAEnabled: boolean) => {

packages/no-modal/src/base/hooks/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export interface IBaseWeb3AuthHookContext {
77
isInitialized: boolean;
88
isInitializing: boolean;
99
initError: unknown;
10-
isAuthenticated: boolean;
10+
isConnected: boolean;
11+
isMFAEnabled: boolean;
1112
provider: IProvider | null;
1213
status: CONNECTOR_STATUS_TYPE | null;
1314
getPlugin(pluginName: string): IPlugin | null;

packages/no-modal/src/base/wallet/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@ export const MULTI_CHAIN_CONNECTORS = {
22
AUTH: "auth",
33
WALLET_CONNECT_V2: "wallet-connect-v2",
44
SFA: "sfa",
5-
};
5+
} as const;
66

77
export const SOLANA_CONNECTORS = {
88
...MULTI_CHAIN_CONNECTORS,
9-
};
9+
} as const;
1010

1111
export const EVM_CONNECTORS = {
1212
COINBASE: "coinbase",
1313
METAMASK: "metamask",
1414
...MULTI_CHAIN_CONNECTORS,
15-
};
15+
} as const;
1616

1717
export const WALLET_CONNECTORS = {
1818
...EVM_CONNECTORS,
1919
...SOLANA_CONNECTORS,
20-
};
20+
} as const;
21+
2122
export type WALLET_CONNECTOR_TYPE = (typeof WALLET_CONNECTORS)[keyof typeof WALLET_CONNECTORS];
2223
export type SOLANA_CONNECTOR_TYPE = (typeof SOLANA_CONNECTORS)[keyof typeof SOLANA_CONNECTORS];
2324
export type EVM_CONNECTOR_TYPE = (typeof EVM_CONNECTORS)[keyof typeof EVM_CONNECTORS];

packages/no-modal/src/noModal.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SafeEventEmitter, type SafeEventEmitterProvider } from "@web3auth/auth";
22
import deepmerge from "deepmerge";
33

4-
import { authConnector } from "@/core/auth-connector";
4+
import { authConnector, AuthLoginParams } from "@/core/auth-connector";
55
import {
66
CHAIN_NAMESPACES,
77
ChainNamespaceType,
@@ -44,6 +44,14 @@ const CONNECTOR_CACHE_KEY = "Web3Auth-cachedConnector";
4444

4545
const CURRENT_CHAIN_CACHE_KEY = "Web3Auth-currentChain";
4646

47+
export type LoginParamMap = {
48+
[WALLET_CONNECTORS.AUTH]: Partial<AuthLoginParams>;
49+
[WALLET_CONNECTORS.METAMASK]: Partial<UserInfo>;
50+
[WALLET_CONNECTORS.COINBASE]: Partial<UserInfo>;
51+
[WALLET_CONNECTORS.WALLET_CONNECT_V2]: Partial<UserInfo>;
52+
[WALLET_CONNECTORS.SFA]: Partial<UserInfo>;
53+
};
54+
4755
export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> implements IWeb3Auth {
4856
readonly coreOptions: IWeb3AuthCoreOptions;
4957

@@ -191,7 +199,7 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
191199
* Connect to a specific wallet connector
192200
* @param connectorName - Key of the wallet connector to use.
193201
*/
194-
async connectTo<T>(connectorName: WALLET_CONNECTOR_TYPE, loginParams?: T): Promise<IProvider | null> {
202+
async connectTo<T extends WALLET_CONNECTOR_TYPE>(connectorName: T, loginParams?: LoginParamMap[T]): Promise<IProvider | null> {
195203
const connector = this.getConnector(connectorName, (loginParams as { chainNamespace?: ChainNamespaceType })?.chainNamespace);
196204
if (!connector || !this.commonJRPCProvider)
197205
throw WalletInitializationError.notFound(`Please add wallet connector for ${connectorName} wallet, before connecting`);
@@ -213,6 +221,7 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
213221
async logout(options: { cleanup: boolean } = { cleanup: false }): Promise<void> {
214222
if (this.status !== CONNECTOR_STATUS.CONNECTED || !this.connectedConnector) throw WalletLoginError.notConnectedError(`No wallet is connected`);
215223
await this.connectedConnector.disconnect(options);
224+
this.connectTo(WALLET_CONNECTORS.METAMASK, {});
216225
}
217226

218227
async getUserInfo(): Promise<Partial<UserInfo>> {
@@ -477,7 +486,7 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
477486
}
478487

479488
this.commonJRPCProvider.updateProviderEngineProxy(finalProvider);
480-
this.connectedConnectorName = data.connector;
489+
this.connectedConnectorName = data.connector as WALLET_CONNECTOR_TYPE;
481490
this.status = CONNECTOR_STATUS.CONNECTED;
482491
this.cacheWallet(data.connector);
483492
log.debug("connected", this.status, this.connectedConnectorName);

packages/no-modal/src/react/no-modal/context/Web3AuthInnerContext.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
1313

1414
const [isInitializing, setIsInitializing] = useState<boolean>(false);
1515
const [initError, setInitError] = useState<Error | null>(null);
16-
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(false);
16+
const [isConnected, setIsConnected] = useState<boolean>(false);
1717
const [provider, setProvider] = useState<IProvider | null>(null);
1818
const [isInitialized, setIsInitialized] = useState<boolean>(false);
1919
const [status, setStatus] = useState<CONNECTOR_STATUS_TYPE | null>(null);
20+
const [isMFAEnabled, setIsMFAEnabled] = useState<boolean>(false);
2021

2122
const getPlugin = useCallback(
2223
(name: string) => {
@@ -29,7 +30,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
2930
useEffect(() => {
3031
const resetHookState = () => {
3132
setProvider(null);
32-
setIsAuthenticated(false);
33+
setIsConnected(false);
3334
setStatus(null);
3435
};
3536

@@ -60,8 +61,6 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
6061
};
6162
}, [web3Auth]);
6263

63-
// TODO: don't throw error in init, connect in v9
64-
6564
useEffect(() => {
6665
const notReadyListener = () => setStatus(CONNECTOR_STATUS.NOT_READY);
6766
const readyListener = () => {
@@ -73,13 +72,13 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
7372
// we do this because of rehydration issues. status connected is fired first but web3auth sdk is not ready yet.
7473
if (web3Auth.status === CONNECTOR_STATUS.CONNECTED) {
7574
setIsInitialized(true);
76-
setIsAuthenticated(true);
75+
setIsConnected(true);
7776
setProvider(data.provider);
7877
}
7978
};
8079
const disconnectedListener = () => {
8180
setStatus(web3Auth.status);
82-
setIsAuthenticated(false);
81+
setIsConnected(false);
8382
setProvider(null);
8483
};
8584

@@ -89,6 +88,11 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
8988
const errorListener = () => {
9089
setStatus(CONNECTOR_STATUS.ERRORED);
9190
};
91+
92+
const mfaEnabledListener = async (isMFAEnabled: boolean) => {
93+
setIsMFAEnabled(isMFAEnabled);
94+
};
95+
9296
if (web3Auth) {
9397
// web3Auth is initialized here.
9498
setStatus(web3Auth.status);
@@ -98,6 +102,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
98102
web3Auth.on(CONNECTOR_EVENTS.DISCONNECTED, disconnectedListener);
99103
web3Auth.on(CONNECTOR_EVENTS.CONNECTING, connectingListener);
100104
web3Auth.on(CONNECTOR_EVENTS.ERRORED, errorListener);
105+
web3Auth.on(CONNECTOR_EVENTS.MFA_ENABLED, mfaEnabledListener);
101106
}
102107

103108
return () => {
@@ -108,22 +113,24 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
108113
web3Auth.off(CONNECTOR_EVENTS.DISCONNECTED, disconnectedListener);
109114
web3Auth.off(CONNECTOR_EVENTS.CONNECTING, connectingListener);
110115
web3Auth.off(CONNECTOR_EVENTS.ERRORED, errorListener);
116+
web3Auth.off(CONNECTOR_EVENTS.MFA_ENABLED, mfaEnabledListener);
111117
}
112118
};
113119
}, [web3Auth]);
114120

115121
const value = useMemo(() => {
116122
return {
117123
web3Auth,
118-
isAuthenticated,
124+
isConnected,
119125
isInitialized,
120126
provider,
121127
status,
122128
isInitializing,
123129
initError,
130+
isMFAEnabled,
124131
getPlugin,
125132
};
126-
}, [web3Auth, isAuthenticated, isInitialized, provider, status, getPlugin, isInitializing, initError]);
133+
}, [web3Auth, isConnected, isInitialized, provider, status, getPlugin, isInitializing, initError, isMFAEnabled]);
127134

128135
return createElement(Web3AuthInnerContext.Provider, { value }, children);
129136
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
export * from "./useCheckout";
22
export * from "./useEnableMFA";
3+
export * from "./useIdentityToken";
34
export * from "./useManageMFA";
45
export * from "./useSwap";
56
export * from "./useSwitchChain";
6-
export * from "./useUserToken";
77
export * from "./useWalletConnectScanner";
88
export * from "./useWalletServicesPlugin";
99
export * from "./useWalletUI";
1010
export * from "./useWeb3Auth";
11-
export * from "./useWeb3AuthAccount";
1211
export * from "./useWeb3AuthConnect";
1312
export * from "./useWeb3AuthDisconnect";
13+
export * from "./useWeb3AuthUser";

packages/no-modal/src/react/no-modal/hooks/useCheckout.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ export interface IUseCheckout {
1212
}
1313

1414
export const useCheckout = (): IUseCheckout => {
15-
const { plugin, isConnected } = useWalletServicesPlugin();
15+
const { plugin, ready } = useWalletServicesPlugin();
1616
const [loading, setLoading] = useState(false);
1717
const [error, setError] = useState<Web3AuthError | null>(null);
1818

1919
const showCheckout = useCallback(
2020
async (showCheckoutParams?: BaseEmbedControllerState["showCheckout"]) => {
2121
if (!plugin) throw WalletServicesPluginError.notInitialized();
22-
if (!isConnected) throw WalletServicesPluginError.walletPluginNotConnected();
22+
if (!ready) throw WalletServicesPluginError.walletPluginNotConnected();
2323

2424
setLoading(true);
2525
setError(null);
@@ -31,7 +31,7 @@ export const useCheckout = (): IUseCheckout => {
3131
setLoading(false);
3232
}
3333
},
34-
[plugin, isConnected]
34+
[plugin, ready]
3535
);
3636

3737
return { loading, error, showCheckout };

packages/no-modal/src/react/no-modal/hooks/useUserToken.ts renamed to packages/no-modal/src/react/no-modal/hooks/useIdentityToken.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { Web3AuthError } from "@/core/base";
44

55
import { useWeb3AuthInner } from "./useWeb3AuthInner";
66

7-
export interface IUseUserToken {
7+
export interface IUseIdentityToken {
88
loading: boolean;
99
error: Web3AuthError | null;
1010
token: string | null;
1111
authenticateUser: () => Promise<string | null>;
1212
}
1313

14-
export const useUserToken = () => {
14+
export const useIdentityToken = () => {
1515
const { web3Auth } = useWeb3AuthInner();
1616

1717
const [loading, setLoading] = useState(false);

packages/no-modal/src/react/no-modal/hooks/useSwap.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ export interface IUseSwap {
1212
}
1313

1414
export const useSwap = (): IUseSwap => {
15-
const { plugin, isConnected } = useWalletServicesPlugin();
15+
const { plugin, ready } = useWalletServicesPlugin();
1616
const [loading, setLoading] = useState(false);
1717
const [error, setError] = useState<Web3AuthError | null>(null);
1818

1919
const showSwap = useCallback(
2020
async (showSwapParams?: BaseEmbedControllerState["showSwap"]) => {
2121
if (!plugin) throw WalletServicesPluginError.notInitialized();
22-
if (!isConnected) throw WalletServicesPluginError.walletPluginNotConnected();
22+
if (!ready) throw WalletServicesPluginError.walletPluginNotConnected();
2323

2424
setLoading(true);
2525
setError(null);
@@ -31,7 +31,7 @@ export const useSwap = (): IUseSwap => {
3131
setLoading(false);
3232
}
3333
},
34-
[plugin, isConnected]
34+
[plugin, ready]
3535
);
3636

3737
return { loading, error, showSwap };

packages/no-modal/src/react/no-modal/hooks/useWalletConnectScanner.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ export interface IUseWalletConnectScanner {
1212
}
1313

1414
export const useWalletConnectScanner = (): IUseWalletConnectScanner => {
15-
const { plugin, isConnected } = useWalletServicesPlugin();
15+
const { plugin, ready } = useWalletServicesPlugin();
1616
const [loading, setLoading] = useState(false);
1717
const [error, setError] = useState<Web3AuthError | null>(null);
1818

1919
const showWalletConnectScanner = useCallback(
2020
async (showWalletConnectScannerParams?: BaseEmbedControllerState["showWalletConnect"]) => {
2121
if (!plugin) throw WalletServicesPluginError.notInitialized();
22-
if (!isConnected) throw WalletServicesPluginError.walletPluginNotConnected();
22+
if (!ready) throw WalletServicesPluginError.walletPluginNotConnected();
2323

2424
setLoading(true);
2525
setError(null);
@@ -31,7 +31,7 @@ export const useWalletConnectScanner = (): IUseWalletConnectScanner => {
3131
setLoading(false);
3232
}
3333
},
34-
[plugin, isConnected]
34+
[plugin, ready]
3535
);
3636

3737
return { loading, error, showWalletConnectScanner };

0 commit comments

Comments
 (0)