Check if MetaMask is Locked

If you are building a DApp and need to create a display to let users know the status of MetaMask, here is some quick code to get you started


function checkMetaMask() {
  // Metamask is not installed
  if (typeof web3 === 'undefined') {
   this.notInstalled = true;
   return;
  }
  
  // Metamask is locked
  const account = web3.eth.accounts[0];
  if (!account) { 
   // locked
  }
},

Per the offical docs, it looks like you should set an interval to keep checking: https://github.com/MetaMask/faq/blob/master/DEVELOPERS.md#ear-listening-for-selected-account-changes

And that’s it!