Losing "connection" with MetaMask

Hi. I’m using "moralis": "^0.0.124", "react-moralis": "^0.2.8".

It seems that under some scenarios I lose “connection” with MetaMask. It happens, for example, if I originally authenticated with MetaMask at the root of the site, and then go to a different route like /mynfts. In the new page, Moralis still tells me that the user isAuthenticated, user.attributes.accounts gives me the correct user address, but MetaMask just doesn’t prompt anymore when I call an SDK function that triggers a transaction (like minting an NFT).

I have to Clear Site Data in Chrome to be able to be unauthenticated again, and start the process all over again to be able to prompt MetaMask.

For example, in the /mynfts route I have this button. When I click on it, Chrome console shows

User authenticated? True
<correct user address>

but I get a 400 Bad Request error, code: 141, error: 'required param address not provided'}

<button onClick={
    async () => {
        console.log("User authenticated? " + isAuthenticated)
        const result = await web3Api.account.getNFTsForContract({
            chain: 'bsc testnet',
            token_address: contractAddress
>Fetch NFTs</button>

Even if I go back to the root of the site, where some functions were correctly prompting MetaMask before changing the route, those same functions doesn’t prompt MetaMask anymore.

Any help will be appreciated!

it looks like you loose connection to web3, isAuthenticated doesn’t track MetaMask connection, it is a different thing, it has a session that it keeps to know that user was authenticated.

it looks like you’ll have to use something like Moralis.enableWeb3() on every page.

As far as I understand, the React-Moralis authenticate() function (given by the useMoralis() hook) creates the Session in the Moralis server but it first prompts the user to connect the MetaMask wallet to the site (necessary condition, because the user needs to sign the authentication transaction).

If I enable Web3 via Moralis.enableWeb3() in every page (btw I’m using Next.js), the user gets prompted by MetaMask instantly when the site loads, for obvious reasons. So, why would I need a Moralis Session for the user? Is it only necessary if I want to use Cloud Functions in my server?

In my specific case, I have no Cloud Functions, I just want to mint NFTs calling a contract and fetch the NFTs owned by the user’s address via Moralis Web3 API (using useWeb3ExecuteFunction() and web3Api.account.getNFTsForContract(), respectively).

Hope I expressed my question correctly.

You don’t necessarily need authentication if that is all you want to do.

What version of Moralis SDK are you using?

Hi @cryptokid, thanks for your response.

I’m using:
moralis 0.0.124,
react-moralis 0.2.8.

You can use Moralis.enableWeb3() before user needs to make a transaction

But also if I want to call the Web3 API, without the user making any transactions.
For example, to call getNFTsForContract(), I need Moralis.Web3() and there’s no transaction involved (otherwise, the address parameter is not automatically passed in the call).

Yes, that is also a case when you need the address, you could save it once in a variable, or call enableWeb3() when needed. For me it doesn’t pop up MetaMask every time I call Moralis.enableWeb3() in vanilla js.

Exactly, MetaMask pops up for every transaction that needs to be signed, or when Moralis.enableWeb3() is called and the MetaMask account was not connected yet.

Do you think calling Moralis.enableWeb3() inside an if statement is a bad practice?

I would say that it is fine to call Moralis.enableWeb3() in a if statement.