Connect MetaMask after Login

Hello,

I am working on a web app, where first user will create the profile, and then he/she will connect metamask. i dunno how to do that, as Moralis.authenticate() creates a new user session, but i want to connect the wallet to the existing user session.

Thanks in advance.

1 Like

try something like this

init = async () => {
   
    window.web3 = await Moralis.Web3.enable();
    initUser();
    
}

initUser = async () => {
    if (await Moralis.User.current()) {
        
         //do stuff such as dispaly eth address etc
       
    }
}

login = async () => {

    try {
        await Moralis.Web3.authenticate();
        initUser();
    }catch (error) {
        alert(error)
    }
}

logout = async () => {
    await Moralis.User.logOut();
    initUser();
}

init()
connectButton.onclick = login

this is a very basic and simple implementation just to give you an idea youlll need go work on this. but using the User.current you can presisit the sessiom on page load i believe.

1 Like

you can also try Moralis.link(eth_address) after you got the eth_address for current wallet (usually after using Moralis.enableWeb3())