onWeb3Enabled is not giving me the correct result

Hi, could I get some help with this, please?

I tried to use the Moralis.onWeb3Enabled() event listener

// Subscribe to onWeb3Enabled events
const unsubscribe = Moralis.onWeb3Enabled((result) => {
  console.log(result);
});

// Unsubscribe to onWeb3Enabled events
unsubscribe();

but I get this result instead when I call unsubscribe()
749f4849d3942ac1dcf053ecfddc4535

I do not get the account, chain, etc. like in the docs
{ account: "0x1a2b3c4d....", chain: "0x1", ...

Btw just wondering, why is it called unsubscribe? is it not supposed to subscribe to an event?

1 Like

have u enabled web3 before calling unsubscribe. from the docs this event will get emmited only after web3 is enabled? or have you already enabled web3?

1 Like

Yes it is defined in a function

const web3Provider = await Moralis.enableWeb3();
unsubscribe = Moralis.onWeb3Enabled((result) => result);

then in console I call unsubscribe() but I still get this result

1 Like

can you check if enableWeb3 worked. this is weird the object return data saying no events emmitted but there should. so can u check web3 is enabled correctly with

const account = await Moralis.account;
console.log(account)

I checked and I get my current address. When I call enableWeb3 is also normal I think
const web3Provider = await Moralis.enableWeb3();
3db858ff509102e9b02d01a06ce2c8fd

1 Like

going to see if i can reproduce hold on gonna call this on my machine and see what i get. ill get back to u soon as i do

1 Like

Hello guys, any news on this?

1 Like

omggg @Markus-55 im soooo sorry i meant to do this last night but i already had three moralis servers and they were all archived had to restore one of them delete it and make a new one which took ages so didnt get time to do it last night. then completelel forgot when i wokr up today im soo sorry about this. didnt mean to keep you waiting.

so yeah i spun up a simple project. and it works as expected for me

thats the output from calling unsubscribe() fro me and works as expected. so it order to try debug your case i have a few quesions

are you using vanilla js. (this event call only works in vanilla js as were using Moralis.) and are you calling this function, and are you inniting your user as per `let user = Moralis.User.current();``. then calling unsubsctibe()

1 Like

code to reporoduce

const serverUrl = "xxxx";
const appId = "xxxx";
Moralis.start({ serverUrl, appId });

/* Authentication code */
async function login() {

    const unsubscribe = Moralis.onWeb3Enabled((result) => {
        console.log(result);
      });
  let user = Moralis.User.current();
  if (!user) {
    user = await Moralis.authenticate({
      signingMessage: "Log in using Moralis",
    })
      .then(function (user) {
        console.log("logged in user:", user);
        console.log(user.get("ethAddress"));
        unsubscribe()
      })
      .catch(function (error) {
        console.log(error);
      });
  }
}

async function logOut() {
  await Moralis.User.logOut();
  console.log("logged out");
}

document.getElementById("btn-login").onclick = login;
document.getElementById("btn-logout").onclick = logOut;

the event will get emmitted whenever metamsk is prompted an opens up

1 Like

Hi thanks a lot, no problem at all! I will check that out :+1:

Hi again, I tried it like in your example and it worked. I think I understand why it did not work for me, it’s because in my code, I first logged in the user, and then after the user was logged in I set const unsubscribe = ...onWeb3Enabled in the console and then called unsubscribe().

What I understood is that, onWeb3Enabled will only get called, when a user is authenticating and it will not work after an authentication has been finished, i.e in my case in the console after I have logged in.

1 Like