0

Hey i'm using Html/Javascript to connect the Metamask. I use "ethereum.request({method: "eth_requestAccounts"})" to connect the Metamask accounts. It get me connected to my metamask account at first when i ran it first time in the browser.

But when i disconnect the account and tried to reconnect using the same connect button the metamask doesn't popup for connecting the accounts. I also did refresh and tries to connect it again with the same connect button but nothing happens.

This is my code:

async function connect(){
    if (typeof window.ethereum !== "undefined"){
        await ethereum.request({method: "eth_requestAccounts"})
        document.getElementById("connectButton").innerHTML = "Connected!"
    } else {
        document.getElementById("connectButton").innerHTML = "Please install the Metamask"
    }}

1 Answer 1

0

If you want to be able to connect to your MetaMask accounts multiple times, you can use the ethereum.enable() method instead of the eth_requestAccounts method.

Like that:

async function connect(){
    if (typeof window.ethereum !== "undefined"){
        try {
            await ethereum.enable();
            document.getElementById("connectButton").innerHTML = "Connected!";
        } catch (error) {
            console.error(error);
            document.getElementById("connectButton").innerHTML = "Error connecting to MetaMask";
        }
    } else {
        document.getElementById("connectButton").innerHTML = "Please install the MetaMask";
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.