How to make your dapp prompt a user to change account connection

Developers have asked how they can their dapp to prompt a user to change the account they are using within MetaMask. Here is a solution.

pi0neerpat opened an issue on July 14, 2020 on the MetaMask extension GitHub repository and made the following request:

My actual issue is the second example. I have a “disconnect” button, which will log a user out, so they can log in again with a different wallet. My dapp is designed for using 1 wallet at a time, in order to simplify things.
Megan has logged in using MetaMask account1. She now wants to use MetaMask account2. She clicks the Disconnect button, and returns to the login page. When she selects MetaMask, the dapp calls .enable() however she is not prompted to select a different account. She has no other option than to go into her MetaMask settings and manually disconnect the dapp.
If I could call disconnect with the Ethereum provider API, this would allow Megan to go through the initial .enable() flow again, without needing to use the MetaMask settings.

The answer that Dan Finlay provided

On a second look, it turns out the method you want is wallet_requestPermissions to re-prompt regardless of previously granted permissions.
So:

const permissions = await ethereum.request({
  method: 'wallet_requestPermissions',
  params: [{
    eth_accounts: {},
  }]
});

pi0neerPat pasted the completed code, along with gifs of how the process works here, with the code used reproduced below:

   const walletAddress = await window.ethereum.request({
  method: "eth_requestAccounts",
  params: [
    {
      eth_accounts: {}
    }
  ]
});

if (!isReturningUser) {
// Runs only they are brand new, or have hit the disconnect button
  await window.ethereum.request({
    method: "wallet_requestPermissions",
    params: [
      {
        eth_accounts: {}
      }
    ]
  });
}