简体   繁体   English

断开 dapp 与 MetaMask 的连接

[英]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:有各种教程教如何激活和停用 Metamask 钱包,或者一般来说可能是任何 CryptoWallet,但在所有这些教程中我都有同样的问题,如下所示:

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:当我停用我的帐户时,它仍会在 Metamask GUI 中显示您已连接,问题是当您重新加载页面时,您再次获得连接行为,就好像您从未断开连接一样,您可以通过以下代码示例自行验证:

using UseDapp framework:使用 UseDapp 框架:

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.使用 Web3-react 教程提供了相同的场景,尽管当我重新加载页面时它没有显示钱包已连接,而是仅显示在 Metamask GUI 上。 My question is a general one, how do you Properly disconnect the Metamask wallet from the dapp?我的问题是一般性的,你如何正确断开 Metamask 钱包与 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您可以在提供的图像中看到行为,钱包通过断开按钮断开连接,但您仍然可以在 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].我也遇到过这个问题,但我发现 dapp 应用程序无法从 Metmask 内部断开连接[它没有特权]。 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.但是当有人真正断开他/她的帐户与 Metamask 的连接时,您可以收听 accountChanged 事件以在您的应用程序中处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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