Error: Moralis auth failed

This error occurs when I tried to login to mobile app using metamask.

error: Moralis auth failed, invalid data {ā€œcodeā€:101,ā€œstackā€:ā€œError: Moralis auth failed, invalid data\n at /workspace/build/auth/MoralisEthAdapter.js:33:15\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async Promise.all (index 0)ā€}

you tried the code from here or something else?

I used this

you used that with a self hosted server?

yes I provided my appID and server address of self hosted server

you may have to adapt the code to be similar to the one that I posted here

ok thanks. Iā€™ll try it

1 Like

I adapted the code you mentioned to react-native and used it to connect to metamask. But it doesnā€™t help. Metamask is not opening when clicked on ā€œAuthenticate via Metamaskā€. There were no error. But the await on enableWeb3({throwOnError: true, provider}); is not resolving.

Opening MetaMask and signing the message should work the same as before. You may have to use directly ethers.

What is the code you are trying based on that React tutorial and in which file of the React Native boilerplate?

If we change the message in authentication at that time authentication fail so any idea how to that

I donā€™t really know on what you are referring. Can you rephrase the question? You can also create a separate forum thread

const handleAuth = async (

    provider:

      | "metamask"

      | "coinbase"

      | "walletconnect"

      | "magicLink"

      | "web3Auth" = "metamask"

  ) => {

    activateInjectedProvider(provider);

    try {

      setAuthError(null);

      setIsAuthenticating(true);

      // Enable web3 to get user address and chain

      await enableWeb3({ throwOnError: true, provider });

      const { account, chainId } = Moralis;

      if (!account) {

        throw new Error(

          "Connecting to chain failed, as no connected account was found"

        );

      }

      if (!chainId) {

        throw new Error(

          "Connecting to chain failed, as no connected chain was found"

        );

      }

      // Get message to sign from the auth api

      const { message } = await Moralis.Cloud.run("requestMessage", {

        address: account,

        chain: parseInt(chainId, 16),

        networkType: "evm",

      });

      // Authenticate and login via parse

      await authenticate({

        signingMessage: message,

        throwOnError: true,

      });

    } catch (error) {

      setAuthError(error);

      alert("Authentication failed Please try again !");

    } finally {

      setIsAuthenticating(false);

      setIsAuthOpen(false);

    }

  };

So here in authenticate if I am giving any hardcoded message like ā€œPlease sign inā€ then it shows an error moralis auth fail

so any idea how can i add the message ?

who shows that error, what happens? do you get to sign the message and you get the error after that, or you donā€™t even get to sign the message?

what are you using as backend? a self hosted server or an existing moralis sever?

did it work before or you are trying now first time?

Yes i got the sign message after that it shows error
I am using self hosted server
before i was using moralis server in that I was just using authenticate function that was working fine

where from is that error? at what step you get that error?
can you add more logging to see where from is that error?

I have the same problem and it is pretty frustrating and annoyingā€¦
The moralis server was a good way to have crypto authentication from scratch which I needed to build up from scratch after the moralis server became obsoleteā€¦ and deploy to aws which finally worked, but there is no way now of getting it working with the react native boilerplateā€¦ is there any solution for this? I do not even get the message to sign on mobileā€¦ only on web client but I need to get it done again through the boilerplate on mobile, please help!

Have you made the change in your authentication code (to use the Auth API via your self-hosted server)? There are examples here - you may have to adjust it slightly as the React Native boilerplate uses very old versions of moralis / react-moralis.

deploy to aws which finally worked

Make sure the authentication works locally first before deploying (as well as any other parts of the app).

it worked locally with the web client and works fine after deploying the parse server to aws.
But the boilerplate does not even work locally with the parse server hosted locally neither on aws.
So I need to adjust it with the authenticate function? I will try that thanks! I also need to adjust the moralis version on the boilerplate?

You need to make the same type of changes for each client that connects to your self-hosted server e.g. any React app (like parse-server-migration-react-client that already has these changes) as well the React Native boilerplate (which hasnā€™t been updated with these changes).

Try it first with the changes to authentication using the same package versions. If you upgrade moralis / react-moralis, possibly other parts of the app will break.

  • connect the wallet
  • get the walletā€™s address (and just use ethereum or ā€œ0x1ā€ as the chainId)
  • request message to sign
  • sign message with wallet to get signature
  • authenticate with this signature