Refresh site and user when account in metamask is changed

Hi!

i was wondering how can I achieve that the website would refresh and user would sync, each time when I change the account or network in metamask.

Maybe I would have to call the function:
await Moralis.Web3.authenticate();
each time when I change something in the metamask. But how can I know that something was changed in the metamask (network or user)

I’ll appreciate any help. Thanks

1 Like

I have noticed the need for this aswell.
what do you think @CasNWK ?

1 Like

Hwy @dbgoodrich you did a little something along this line.
what input have you got ?

thanks

1 Like

What I was dealing with was a little different so I just created a wait/retry routine that detected if the chain was available.

Not sure if you noticed this message in browser console:

inpage.js:1 MetaMask: MetaMask will soon stop reloading pages on network change.
For more information, see: https://docs.metamask.io/guide/ethereum-provider.html#ethereum-autorefreshonnetworkchange 
Set 'ethereum.autoRefreshOnNetworkChange' to 'false' to silence this warning.

Follow the link provided: https://docs.metamask.io/guide/ethereum-provider.html#table-of-contents This page contains a lot of good information about how to detect wallet availability, network availability, account switches, etc. Especialy check out the Events section of the page. This section has examples for how to detect when the account is changed.

Regards,

David

2 Likes

hey @CasNWK do check this out tooo

1 Like

https://docs.moralis.io/moralis-sdk/web3

Under the paragraph events, there is described something about this.

Here it is. There is a typo in the https://docs.moralis.io/moralis-sdk/web3 (account->accounts)


Moralis.Web3.onAccountsChanged(function(accounts) {

    console.log(accounts);
// your code to run when "accountsChanged" happens

});
1 Like

yes thats the event for listening to account change
You can refresh site inside of that callback

1 Like