Is there any way to initiate a disconnect request to the Metamask wallet?

Viewed 4630

I'm building a decentralized application where users can connect their cryptocurrency wallet (Metamask) to my website.

They can initiate a connection request by clicking a button. On success, the wallet is connected and my website can interact with it.

Is there any way to initiate a disconnect request? Similar to a 'Log out' button. Currently, the users have to manually disconnect their wallet within Metamask settings which is not a straightforward process.

3 Answers

Here is what I use to disconnect your connected account (assuming you have only one) from the application:

await window.ethereum.request({
    method: "eth_requestAccounts",
    params: [{eth_accounts: {}}]
})

Not the answer you might be hoping for: It's not possible.

The connect/disconnect functionality is entirely in the user's hands due to security and privacy concerns. You can kindly ask the wallet to prompt the user to connect to the website, but there is no functionality to prompt the user to disconnect.

Programmatically resetting the accounts array does not disconnect the wallet even though some implementations such as Pancake Swap suggest this is the case; they simply pretend a disconnect.

Related