14

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
vladwho
  • 141
  • 1
  • 4
  • Hi! I know the post is old and you may already found a way to solve the problem, but I found this: https://github.com/MetaMask/metamask-extension/issues/8990#issuecomment-658985565, hope it helps :D – FORGISDELUXE Aug 29 '21 at 17:46

3 Answers3

3

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.

q9f
  • 11,293
  • 8
  • 57
  • 96
2

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: {}}]
})
TylerH
  • 20,799
  • 66
  • 75
  • 101
MetaZebre
  • 769
  • 8
  • 10
1

from this: https://github.com/MetaMask/metamask-extension/issues/8990

const accounts = await window.ethereum.request({
    method: "wallet_requestPermissions",
    params: [{
        eth_accounts: {}
    }]
}).then(() => ethereum.request({
    method: 'eth_requestAccounts'
}))

const account = accounts[0]

Credit to nikita: https://github.com/nikitamarcius

TylerH
  • 20,799
  • 66
  • 75
  • 101
afabei
  • 33
  • 6