Check if User is already logged in Moralis 1.0.4

Hi,

so i was able to almost recode everything in my app for the new Moralis SDK. One last thing is my old “Check if user is already logged in function”

That was the function i used before… but it is not working correctly anymore… after people switching accounts on metamaks for example the currentUser stays the same here.

async function init()
{
  const currentUser = Moralis.User.current();
  if (currentUser) {
    const userAddress = currentUser.get("ethAddress");
   //doing something here
    
  } else {
      //doing something here
  }
}

so i am tried to recode this function to:

async function init()
{
  const currentUser = await Moralis.account;

  if (currentUser) {
    const userAddress = currentUser;
   //doing something here
    
  } else {
      // show the signup or login page
  }
}

but the currentUser here is always null if i refresh the page. Do i miss something here?

greetings

are you sure that this code worked with the previous SDK?

I don’t think that currentUser.get("ethAddress") updates when user changes account in metamask

yeah it worked perfectly the last 2 months ^^ but never the less with the new SDK the currentUser.get(“ethAddress”) is useless … so in general i need a solution to check if the user is already logged in and get the current address so i cen preload the page with his datas.

can you try to update to latest version of the SDK?

is the walletconnect problem solved on the latest one?

I don’t know exactly, latest version seem to be 1.1.0

ok updated to 1.1.0, walletconnect problem seems to be fixed, as its working. but the other stuff still behave the same… i am connecting metamaks -> datas loading perfectly -> switching account -> datas loading perfectly -> refresh page -> first account appears instead of the second one wich was the last one used

what you mean by switching account? only switching in metamask or also logout and login user with the new account?

just switching accounts on metamask / trustwallet … function onAccountChanged sees it and loads the new datas for the dapp. but once the page gets refreshed and i check if the user is already logged in the address is null even if i did not log out

there may be different things switching accounts in metamask and logging in a user

it may be easier to read the ETH Address from web3 or ethers instance, in theory if you didn’t logout and login then the original address is still associated with the user even if accounts changed in metamask so, on refresh you will get one of the two addresses or none