Disconnect a web3 wallet [Solved]

Hi,

I’m looking for the correct function call to disconnect my dapp from a web3 session, specifically when using walletconnect but also with a metamask connection would be handy. I’ve read all the docs, and also scoured the internet (trying various combinations of killSession(), disconnect() etc. What I am looking to implement is the ability for my users to explicitly disconnect the wallet connection (much as uniswap does with it’s ‘disconnect’ option.

Any advice gratefully received.

Thanks!

1 Like

All credit to today’s hero @Erno on discord, but for anyone else stuck here is the answer:

What you can do is access the WalletConnectProvider (once a connection has been made), via Moralis.Web3.activeWeb3Provider.provider. I think you can find the methods you need on that object. For instance, you could use .deactivate(). (make sure that Moralis.Web3.activeWeb3Provider.provider is defined, as this is only set after enabling web3 / authenticating)

It will return a walletconnect web3 provider instance, documented in https://docs.walletconnect.org/quick-start/dapps/web3-provider

2 Likes

I couldn’t get the above approach to work, but it set me on the right path for sure! I have found the below code to be effective (obviously not called one after the other. . . :)):

window.web3 = await new window.Moralis.Web3.enable({ provider: "walletconnect" });

await window.web3.eth.currentProvider.disconnect();
1 Like

@keyoke Amazing work! You can further look into Moralis documentation for Events : https://docs.moralis.io/moralis-server/web3/web3-1#events

onDisconnect Event will help you watch disconnect your wallet

const disconnect = Moralis.Web3.onDisconnect(function(accounts) {
  console.log(account);
});

Any questions and queries feel free to ask

Thanks @taha, I really appreciate the reply. I’d read that doc in detail (love all the docs, great job team moralis), and that’s a wrapper for watching an event, not calling the disconnect method itself. I need something to call disconnect, not watch for the event. I think what you are proposing is a different thing to my requirement.

Apologies for not understanding your requirement.

Yes, you can use web3.eth.currentProvider.disconnect() to disconnect from wallet and onDisconnect to add listener to the disconnect.

Thanks

Exciting! Looking through the Moralis js code and I found the function ‘cleanup’. The below works very nicely to close a web3 connection:

await window.Moralis.Web3.cleanup();

1 Like

is this still the same approach when using Metamask only?

Disconnecting metamask is not working in the latest moralis, react-moralis packages.
Who can send me the code how to disconnect metamask using web3 or Moralis api?

Also, I heard it is impossible to disconnect metamask from connected website forcely.

Yes you can’t force the user to add or remove a site from their connected sites in MetaMask. You can disconnect the wallet connection from an app perspective.

This is web3uikit’s disconnect wallet code for example:

window.localStorage.removeItem('provider');
setWeb3Status('disconnected');
deactivateWeb3();
if (isInitialized) logout();

setWeb3Status is not function, I am getting this error.
const { logout, isAuthenticated, Moralis, chainId, account, user, deactivateWeb3, setWeb3Status, } = useMoralis();

In that example, setWeb3Status is a state from React useState. So it’s just some way to represent the user’s connection state e.g. if web3Status is connected, display the user’s address on the screen. This will be different for your app.

window.localStorage.removeItem(‘provider’);
deactivateWeb3();
if (isInitialized) logout();

I have run this code but still remained connection in metamask.

This is fine, that Connected state represents the connected site for that particular wallet in MetaMask. You can’t remove or disconnect this in code, only the user can do this.

1 Like

Hello I have came across this same problem for VanillaJS as well.

Moralis.User.logOut()

in the screenshot is what’s inside of the Moralis.User object, you can see that there is a logOut function there that you can use.

image