EthAddress not getting correct address

So I have metamask which correctly works and shows correct ethAddress when logged in but after logging in if I change metamask address it doesn’t displays the new ethAddress and still displays the other address.
I’m doing this just for testing purposes.
Here’s my code:

//displaying eth address

          Moralis.User.currentAsync().then(function(user) {

            var wallet = user.get('ethAddress');

            document.getElementById('address').innerHTML = wallet;

          });

In dashboard I do have both address and it will show the correct address upon logging in regardless the ethAddress used, but after logging in if I change wallet address it doesn’t show the new address

Moralis.onAccountsChanged( async (accounts) => {

            const confirmed = confirm("Link this address to your account?");

            if (confirmed) {

                await Moralis.link(accounts[0]);

            }

          });

I also added this to confirm link and ask SignIn through metamask but it still displays old ethAddress

1 Like

the address from user object (user.get('ethAddress')) can be different than the address that is selected in MetaMask, it is not the same thing. One is the address that it is associated with the user account (also used for current session) and one is the address that is selected now in metamask. You can get both of those addresses with a different syntax.

I SOLVED IT!!!

After spending whole day going from articles to articles, adding and deleting all code/possibilities I got it to work!!!

Here’s my code for future readers:

//ethaddress change + display

          Moralis.onAccountsChanged( async (accounts) => {

            const confirmed = confirm("Link this address to your account?");

            if (confirmed) {

               user = await Moralis.authenticate();

               window.location.href = "profile.html";

               await Moralis.link(accounts[0]);

            } else {

                await Moralis.User.logOut();

                window.location.href = "index.html";

            }

          });

Now I don’t know if this is the only way to do this, or even if this is the right way to do this but as long as it delivered the results I wanted I’m super happy!!.

Now I can focus on solving my 2nd problem.
Happy Coding!!.

This did not worked :sob::sob::sob::sob::sob:
This created a new user everytime I changed ethAddress, which I don’t know if is a good or not so good but at the time being even complicated the problem for me so I commented it out for now

this creates a new user if it doesn’t already exist

Right so…THIS WORKS!!!

//ethaddress change

          Moralis.onAccountsChanged( async (accounts) => {

            const confirmed = confirm("Link this address to your account?");

            if (confirmed) {

                var user = await Moralis.link(accounts[0]);

                document.getElementById('address').innerHTML = await Moralis.link(accounts[0]);

                alert('account changed');

                window.location.reload();

            }

        });

It sets the accounts under same user/ethAddress and upon reloading the page displays the correct address, but that half a sec it takes to reload it displays [Object object] don’t exactly know why but I can live with that for now…!!

I figured. But I corrected it.