How to Detect When a User Changes Their Metamask Account (2024)

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

How to Detect When a User Changes Their Metamask Account (1)

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.

How to Detect When a User Changes Their Metamask Account (2)

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.

Let’s assume that you have a function called getAccount() that connects to Ethereum and loads the active account being used. This occurs whenever the DApp is loaded.

Metamask fires an accountsChanged event upon account selection changing, which we can listen for. Figure 2 shows what the code for this looks like:

Using this event listener enables your DApp to get the latest wallet data in real-time, making your user experience slick and up to date. Figure 3 shows a DApp that retrieves the active wallet address and displays it in a button when the accountsChanged event is fired.

How to Detect When a User Changes Their Metamask Account (3)

For more information on this feature, see Metamask’s documentation.

If you’re interested in Blockchain Development, I write tutorials, walkthroughs, hints, and tips on how to get started and build a portfolio. Check out some of these resources:

How to Detect When a User Changes Their Metamask Account (2024)

FAQs

How to detect account change in MetaMask? ›

In the newer version metamask exposes an event that could be used to detect whether there's an account change as per new doc: window. ethereum. on('accountsChanged', function (accounts) { // Time to reload your interface with accounts[0]! })

Why is there a second account on my MetaMask? ›

What you do need to understand is that your accounts are controlled and created by your Secret Recovery Phrase. Each account is represented by a public address. Each account you create in MetaMask is derived from your Secret Recovery Phrase; therefore, your wallet contains one or more accounts.

How do I check my MetaMask account? ›

Go via the account route: In the MetaMask Extension, click the account dropdown arrow and select the three vertical dots icon for the account you want to view on an explorer.

How do I access my second MetaMask account? ›

MetaMask allows you to manage multiple accounts — that is, multiple public addresses. Here's how you switch between them. Click on the account selector at the top of the screen. Here you'll see each of your previously created or imported accounts; click on the vertical dots next to each to view further details.

Can MetaMask account be tracked? ›

In short: if you're using MetaMask, your IP address and your Ethereum wallet address are getting logged with each transaction you make. But there is more. The moment you unlock the wallet it records ALL your addresses under the same IP. This makes it very easy to identify who you are and where you are located.

Are multiple accounts in MetaMask different wallets? ›

Your accounts in MetaMask are each represented by a public address (the 'public' side of your key pair), which you may have seen referred to incorrectly as a 'wallet address'. There is no such thing: there is only an account address.

Does each MetaMask account have a different seed phrase? ›

It is not possible to create a new account with a new seed phrase within the same wallet. Each wallet is tied to a single seed phrase.

Is it OK to have 2 MetaMask wallets? ›

Yes, you can have two or more Metamask crypto wallets. Within the wallet, you can create multiple addresses that function as a wallet. Each one can be imported to multiple devices as well.

What happens when you reset MetaMask? ›

A wallet reset essentially returns your MetaMask Extension or Mobile app to the state it was in when you downloaded it: it clears your Secret Recovery Phrase and your accounts from the software. After you execute a reset, you'll need to re-enter your Secret Recovery Phrase to get access to your wallet again.

Can you recover a MetaMask account? ›

You can now use the Secret Recovery Phrase (and private keys) to restore your MetaMask wallet.

What is a secret recovery phrase on MetaMask? ›

Your Secret Recovery Phrase (SRP) is a unique 12-word phrase that is generated when you first set up MetaMask. Your funds are connected to that phrase. If you ever lose your password, your SRP allows you to recover your wallet and your funds.

Does each MetaMask account have a different private key? ›

While a Secret Recovery Phrase is used to create and restore your entire MetaMask Wallet, including all accounts created in that wallet, each account has its own private key. This key can be used to import that account, and that account only, into a different wallet.

Are MetaMask accounts linked? ›

Are several MetaMask accounts linked? Addresses are not in any way linked to one another. They can link the different addresses if they have access to your mnemonic phrase, extended private key, or extended public key, but you should never give this information out.

How do I see internal transactions on MetaMask? ›

So if you're looking for a transaction and don't see it anywhere, check under the 'Internal Transactions' tab in Etherscan. To learn how to examine your account on Etherscan, start here. The actual transfer of funds shows up under the “To” field.

How do I catch MetaMask error? ›

To catch MetaMask errors, we have to add a promise after the function call. This promise anticipates the hash of the transaction about to be broadcast to the blockchain.

Top Articles
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 6360

Rating: 4.8 / 5 (48 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.