"MetaMask - RPC Error: Already processing eth_requestAccounts. Please wait." when closing my browser, reopening and trying to login again using the window.ethereum object

I’m using this code to get a user’s public address when they connect their metamask to my website:

 if (typeof window.ethereum !== 'undefined') {
            console.log('MetaMask is installed!');
            const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
            return true;
        } else {
            return false;
        }

Only problem is that the first time it works, but then if I close my browser, reopen it and try to login again, it doesn’t.

I have to manually login to metamask, go to connected sites, remove my website and then try again. Why is that?

How can I make it so this doesn’t happen? It seems only a user can remove the connection manually and I cannot do it through code. What should I do?

Estou com o mesmo problema, preciso de ajuda, o meu esta na rede FTM E DA MATIC e nao sei como resolver, não consigo fazer swap em nenhuma defi, de ambas as redes, se alguem puder me ajudar… preciso urgente… obrigado

no one ever replies, or at least says anything useful, not even in the git repo. just people pointing out the same problems over and over again and no news from metamask

Hey @Mat99, welcome to the MetaMask community! :fox_face:

Try running this:

function connect() {
  ethereum
    .request({ method: 'eth_requestAccounts' })
    .then(handleAccountsChanged)
    .catch((err) => {
      if (err.code === 4001) {
        // EIP-1193 userRejectedRequest error
        // If this happens, the user rejected the connection request.
        console.log('Please connect to MetaMask.');
      } else {
        console.error(err);
      }

The Ethereum Provider API section on the MetaMask docs may help you:

Hi… Thank you, but this didn’t fix it. The problem is not that the user clicks repeatedly the button while waiting for a connection. The problem, is that if a user connected their wallet the first time, when they close the browser and then reopen it, the ethereum.seletedAddress is null, so of course the user has to click the login button again, but upon doing that, nothing happens because I get the error that a request is still pending, even if it isn’t as the user didn’t click any button during that session and the ethereum.selectedAddress is empty.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.