diff --git a/packages/nextjs/services/web3/connectors.tsx b/packages/nextjs/services/web3/connectors.tsx index 82872b75..05584560 100644 --- a/packages/nextjs/services/web3/connectors.tsx +++ b/packages/nextjs/services/web3/connectors.tsx @@ -1,4 +1,4 @@ -import { argent, braavos } from "@starknet-react/core"; +import { argent, braavos, InjectedConnector } from "@starknet-react/core"; import { getTargetNetworks } from "~~/utils/scaffold-stark"; import { BurnerConnector } from "./stark-burner/BurnerConnector"; import scaffoldConfig from "~~/scaffold.config"; @@ -7,6 +7,17 @@ const targetNetworks = getTargetNetworks(); export const connectors = getConnectors(); +// workaround helper function to properly disconnect with removing local storage (prevent autoconnect infinite loop) +function withDisconnectWrapper(connector: InjectedConnector) { + const connectorDisconnect = connector.disconnect; + const _disconnect = (): Promise => { + localStorage.removeItem("lastUsedConnector"); + return connectorDisconnect(); + }; + connector.disconnect = _disconnect.bind(connector); + return connector; +} + function getConnectors() { const { targetNetworks } = scaffoldConfig; @@ -18,7 +29,7 @@ function getConnectors() { connectors.push(new BurnerConnector()); } - return connectors.sort(() => Math.random() - 0.5); + return connectors.sort(() => Math.random() - 0.5).map(withDisconnectWrapper); } export const appChains = targetNetworks;