Authenticate with MetaMask always use the first account and not the selected one

Hi.
When using authenticate from react-moralis package to login a user with MetaMask it will try to authenticate the user with his first account and not the one he selected.

Let’s say a user has 2 accounts configured in MetaMask.
He open the extension and selects account 2.
He then click on the dapp login button.
The dapp calls the authenticate method.
The MetaMask popup comes up but with account 1 selected. The user has no way to change this to account 2.
Basically - there is no way for the user to ever login with any account besides the first one.

1 Like

Actually, this happens just when one of the accounts is already connected. So basically, to allow the user to switch accounts, there is need to be a way to first disconnect the first one. Then MetaMask asks the user to choose accounts.

1 Like

It looks like the user can select an account in MetaMask before pressing the login button.

@cryptokid just if a wallet is not connected already.

I just tested, and I can select any wallet in metamask, but I have to do it before clicking on the authenticate button.

Weird. Doesn’t work for me.
The only way I found working is to use this code to force MetaMask to ask the user to choose an account:

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

Could be nice to have a parameter in the authenticate method that trigger such code.

What I did was to open metamask and select a different account. And after that when clicking on the button to authenticate, it looks like it is using the current account that it is selected in MetaMask.

Hi, i just ran into the same issue.

Topic: Moralis login/logout then change MM account

  1. I’m logged to Moralis with Metamask account 1 - all is good in the world
  2. Logout with await Moralis.User.logOut()
  3. Change Metamask to account 2
  4. Login again to Moralis: eth address is still from account 1 - the world ends

Q A: How does Moralis identify a returning user? (through the sessionToken maybe?)

Q B: How do we ‘totally’ logout so we can properly re-login with another wallet address?

PS: I found this in the Moralis.js code: “This will create a new Parse.User on the server, and also persist the session on disk “

how did you get this information?
in your case you should have two different users, and I guess that you didn’t link 2 addresses to same user

Info through console log

My dApp runs here: https://nft.tnxz.nl/
My code is here: https://github.com/dizid/minter

Also, just now i saw a totally new message from Metamask, before loggin into Moralis:
“Is this the correct account? Its different from the currently selected account in your wallet”

See printscreen.

After this step you will receive a notification in your Metamask “Do you want to switch/connect this account?”

Moralis gets the info about user changes from Metamask. But you need to “allow” Metamask to connect your new wallet address to the site.

You may have problems with this in localhost sometimes

No, i don’t get this notification.

when you change your account on the site you will recieve this message or you should manually add it by clicking on “Not Connected”

I created spagetti-code and need to clean up.
Will report back here tomorrow, if and how solved.

In the meantime, i managed to be connected with 2 accounts :smile:

Schermopname (47)

What I ended up doing is something like this (code can be better, but I just wanted to see that it is working). It basically makes MetaMask ask the user to choose an account if he is trying to add a 2nd wallet to our app.
One thing to note: We probably need to change the 2nd authenticate call to a “link” call from Moralis. Didn’t have time yet to try and use the link feature.

const onConnectWithMoralisAndMetaMask = async () => {
    // If the user is adding his first wallet?
    if (wallets.length === 0) {
      authenticate({
        signingMessage: "Connect wallet with Gallerium",
        onSuccess: onMoralisConnectSuccess,
        onError: () => {
          onwalletFailedToConnect();
        },
      });
    } else {
      const permissions = await window.ethereum.request({
        method: "wallet_requestPermissions",
        params: [
          {
            eth_accounts: {},
          },
        ],
      });

      if (permissions) {
        authenticate({
          signingMessage: "Connect wallet with Gallerium",
          onSuccess: onMoralisConnectSuccess,
          onError: () => {
            onwalletFailedToConnect();
          },
        });
      }
    }
  };

Nice solution, will try it out also.

I figured out that Moralis does not logout / disconnects from MM.
Another solution i’m gonna try is, if user changes MM account, i logout from Moralis and re-login with new MM acc.