Skip to content

Commit 18bcd1b

Browse files
committed
(fix): state mismatch between web3auth and wagmi
1 parent 1fb105c commit 18bcd1b

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

packages/modal/src/react/wagmi/provider.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,19 @@ async function connectWeb3AuthWithWagmi(connector: Connector, config: Config) {
7474
}));
7575
}
7676

77-
async function disconnectWeb3AuthFromWagmi(config: Config) {
77+
function getWeb3authConnector(config: Config) {
78+
return config.connectors.find((c) => c.id === WEB3AUTH_CONNECTOR_ID);
79+
}
80+
81+
function resetConnectorState(config: Config) {
7882
config._internal.connectors.setState((prev) => prev.filter((c) => c.id !== WEB3AUTH_CONNECTOR_ID));
83+
config.connectors.filter((c) => c.id !== WEB3AUTH_CONNECTOR_ID);
84+
}
85+
86+
async function disconnectWeb3AuthFromWagmi(config: Config) {
87+
const connector = getWeb3authConnector(config);
88+
await Promise.all([config.storage?.setItem(`${connector?.id}.disconnected`, true), config.storage?.removeItem("injected.connected")]);
89+
resetConnectorState(config);
7990
config.setState((state) => ({
8091
...state,
8192
chainId: state.chainId,
@@ -95,6 +106,13 @@ function Web3AuthWagmiProvider({ children }: PropsWithChildren) {
95106
onDisconnect: async () => {
96107
log.info("Disconnected from wagmi");
97108
if (isConnected) await disconnect();
109+
110+
const connector = getWeb3authConnector(wagmiConfig);
111+
// reset wagmi connector state if the provider handles disconnection because of the accountsChanged event
112+
// from the connected provider
113+
if (connector) {
114+
resetConnectorState(wagmiConfig);
115+
}
98116
},
99117
});
100118

0 commit comments

Comments
 (0)