简体   繁体   中英

Disconnect dapp from MetaMask

there are various tutorial that teach how to activate and deactivate Metamask wallet, or probably any CryptoWallet in general, but in all of them i have the same problem, as so:

When i deactivate my account it is still displayed in Metamask GUI that you are connected, the problem with that is when you reload the page you get the connected behaviour again, as though you never disconnected, you can verify this yourselves via this code example:

using UseDapp framework:

import React from 'react'
import { useEthers } from '@usedapp/core'

const TryUseDappExample = () => {

  const { activateBrowserWallet, deactivate, account } = useEthers()

  return (
    <div>
      {!account && <button onClick={activateBrowserWallet}> Connect </button>}
      {account && <button onClick={deactivate}> Disconnect </button>}
      {account && <p>Account: {account}</p>}
    </div>
  )
}

export default TryUseDappExample

please try to hit connect -> disconnect -> reload the page, you will see you are still connected.

Using Web3-react tutorial provide the same scenario, although when i reload the page it doesn't show that the wallet is connected, rather it is shown only on Metamask GUI. My question is a general one, how do you Properly disconnect the Metamask wallet from the dapp? Thank You!

You can see the behaviour in the image provided, the wallet is disconnected via disconnect button, but you can still see its connected in Metamask GUI在此处输入图像描述

I have faced this problem too,but I found there is no way for a dapp application to disconnect from inside Metmask[it doesn't have the privilege]. but what we can do is to handle the connection from inside of our application. ex: you store the account and provider inside a variable and then when you press disconnect button it just removes that information from that variable, you think we are disconnected, but in reality we can't disconnect from actual metamask.

but when somebody really disconnects his/her account from Metamask, then you can listen to accountChanged event to handle it in your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM