MoralisWeb3 / react-moralis

Hooks and components to use Moralis in a React app
MIT License
624 stars 166 forks source link

deactivateWeb3 not working (I think) #195

Open PatrickAlphaC opened 2 years ago

PatrickAlphaC commented 2 years ago

New Bug Report

Checklist

Issue Description

I have a component that looks like this:

import { useMoralis } from "react-moralis"

// Top navbar
export default function Navbar() {
  const { enableWeb3, isWeb3Enabled, deactivateWeb3, isWeb3EnableLoading } = useMoralis()

  return (
    <nav>
      <ul>
        <li>
          {isWeb3Enabled ? (
            <button
              onClick={() => {
                deactivateWeb3()
              }}
            >
              Disconnect
            </button>
          ) : (
            <button
              onClick={() => enableWeb3()}
              disabled={isWeb3EnableLoading}
            >
              Connect
            </button>
          )}
        </li>
      </ul>
    </nav>
  )
}

However, when you click Disconnect, the wallet is still connected and doesn't disconnet. What is going on?

Expected Outcome

I expect my metamask to "disconnet" but it stays connected.

Server

Environment

"react-moralis": "^1.2.4",
"moralis": "^1.2.7",

MacOS Montery 12.1 Brave & Chrome browser

PatrickAlphaC commented 2 years ago

Ah... looks like Metamask sort of overloads the term connected. https://docs.metamask.io/guide/ethereum-provider.html#methods

Note that this method has nothing to do with the user's accounts.

You may often encounter the word "connected" in reference to whether a web3 site can access the user's accounts. In the provider interface, however, "connected" and "disconnected" refer to whether the provider can make RPC requests to the current chain.

It might be nice to have a hook for like "disconnect" or something that actually truly disconnects.

PatrickAlphaC commented 2 years ago

Leaving open for visibility on that comment.

ErnoW commented 2 years ago

I am not sure if that's even possible to do with Metamask. As far as I know, it is not possible to disconnect a metamask wallet from code, only via their app-extention