Skip to content

Commit

Permalink
add workaround on wallet connectors to remove from localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadai2010 committed Aug 21, 2024
1 parent 8228537 commit a292ab0
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/nextjs/services/web3/connectors.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<void> => {
localStorage.removeItem("lastUsedConnector");
return connectorDisconnect();
};
connector.disconnect = _disconnect.bind(connector);
return connector;
}

function getConnectors() {
const { targetNetworks } = scaffoldConfig;

Expand All @@ -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;

0 comments on commit a292ab0

Please sign in to comment.