How to Detect When a User Changes Their Metamask Account
How to Detect When a User Changes Their Metamask Account

Make sure you’re tracking the user’s correct ETH account

Photo by Jeison Higuita on Unsplash.

Prerequisite: A beginner-level understanding of Ethereum and web3.js.

User experience is one of the biggest roadblocks to onboarding users onto your decentralised application (DApp). Modern web browsers don’t support web3 by default, so most users have to interact with DApps via the Metamask browser extension.

Metamask allows users to alter the network and change their active wallet address from the extension.

Figure 1

Unfortunately, DApps won’t know when the user changes an account without specifically checking for it. This means that if a user changes accounts whilst using your DApp, they will be presented with out-of-date information relating to the previous account. This is like logging into your Facebook account and seeing the previous user’s dashboard. It is obviously unacceptable.

Fortunately, Metamask now allows you to listen for when a user changes their account in real-time with a simple piece of code.
['Alex Roan']
['Ethereum', 'Cryptocurrency', 'Crypto', 'Programming', 'Blockchain']