Self-hosting your Moralis server

I am dealing with the same issue as mikefilmworks

I am using const {authenticate} = useMoralis() v1.4.2 and I tried both web3auth and standard meta mask and both give the same error

Server error:

warn: DeprecationWarning: auth.moralisEth is deprecated and will be removed in a future version. auth.moralisEth.enabled: true
error: Moralis auth failed, invalid data {"code":101,"stack":"Error: Moralis auth failed, invalid data\n    at S:\\Dropbox\\QuarkStars\\quarkstars-app\\_server\\src\\auth\\MoralisEthAdapter.ts:32:13\n    at runMicrotasks (<anonymous>)\n    
at processTicksAndRejections (internal/process/task_queues.js:95:5)\n    at async Promise.all (index 0)"}

Client side response from localhost:1337/server/user

{"code":101,"error":"Moralis auth failed, invalid data"}

I tried upgrading to Moralis v1.12.0 but the npm package say it doesn’t exist when I do npm install [email protected] --save. I can go to v1.11.0 or to v2.7.4, but 1.12.0 is missing.

I tried upgrading to Moralis v1.12.0 but the npm package say it doesn’t exist when I do npm install [email protected] --save. I can go to v1.11.0 or to v2.7.4, but 1.12.0 is missing.

You need to be using moralis-v1.

Have you adjusted your authentication code?

1 Like

Okay yes, I didn’t see we had to update the authentication code.

I got web3Auth to work by editing the react code on that page like this (I’m not sure why I have to pass all the params to both functions)

  const handleLogin = async () => {
    let authUser: any;
    try {
      setAuthError(null);
      setIsAuthenticating(true);
      // Enable web3 to get user address and chain
      await enableWeb3({ 
        throwOnError: true, 
        provider: "web3Auth", 
        clientId: process.env.NEXT_PUBLIC_WEB3AUTH_ID!,
        rpcTarget: process.env.NEXT_PUBLIC_RPCNODE!,
        appLogo: "https://quarkstars.com/icon-color.png",
        theme: "light", //or "dark"
        loginMethodsOrder: ["google", "facebook", "twitter", "discord", "reddit", "apple", "line", "github", "kakao", "linkedin", "weibo", "wechat", "email_passwordless"]

      });
      const { account, chainId } = Moralis;
      if (!account) {
        throw new Error('Login Failed');
      }
      if (!chainId) {
        throw new Error('Login Failed');
      }
    if (!isAuthenticated && account && chainId) {
      const chainId = parseInt(process.env.NEXT_PUBLIC_CHAINID!)
      //chainId must match the testnet/mainnet of the web3auth clientId's project settings or the login won't work
      // Get message to sign from the auth api
      const { message } = await Moralis.Cloud.run('requestMessage', {
        address: account,
        chain: chainId,
        network: 'evm',
      });
      authUser = await authenticate({
        signingMessage: message,
        throwOnError: true,
        provider: "web3Auth",
        chainId,
        rpcTarget: process.env.NEXT_PUBLIC_RPCNODE!,
        clientId: process.env.NEXT_PUBLIC_WEB3AUTH_ID!,
        appLogo: "https://quarkstars.com/icon-color.png",
        theme: "light", //or "dark"
        loginMethodsOrder: ["google", "facebook", "twitter", "discord", "reddit", "apple", "line", "github", "kakao", "linkedin", "weibo", "wechat", "email_passwordless"]
      })
    }
  } catch (error: any) {
    setAuthError(error);
    setStatus(error?.message);
  } finally {
    setIsAuthenticating(false);
  }
}

Yes you need to use the same options used in enableWeb3() for authenticate() as it also enables web3. This was the same case for hosted servers.

Got it, sounds like there’s alot of documentation to update!

1 Like

BTW: I’m trying out deploying on railway. It stays online indefinately rather than serverless cold starts. works so far.

I have set up the self server and everything was working but now we I want to authenticate ,Heroku server logs shows this error

 error: [U0002] Incorrect network provided. Got "undefined", Valid values are: "evm", "solana" {"code":141,"stack":"Moralis Auth Error: [U0002] Incorrect network provided. Got \"undefined\", Valid values are: \"evm\", \"solana\"\n    at /app/node_modules/@moralisweb3/auth/lib/methods/requestMessage.js:52:19\n    at MoralisAuth._this.requestMessage (/app/node_modules/@moralisweb3/auth/lib/MoralisAuth.js:27:112)\n    at requestMessage (/app/build/auth/authService.js:14:49)\n    at /app/build/cloud/main.js:8:60\n    at /app/node_modules/parse-server/lib/Routers/FunctionsRouter.js:173:16\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"}

and i have passed the “evm” in {message}

you could try to add some logging to see what happens
you could log the parameters for example to see if they have the expected values

Hey Glad. I managed to get a little further. Now my custom cloud function fails (it didn’t before) and just trying to debug why.

It looks like there is no user.get(“accounts”) passed through to the parse server anymore. I was using the accounts array server side to check if

Also, I don’t think the Parse server is returning any data from the collection in MongoDb either (I’ve migrated the data and it looks ok).

can you make a summary of current code that you have?

It’s pretty long to post it all. To be honest, aside from the request.user.get(“accounts”) not being passed to the backend Parse server by default (like it does with Moralis server), I think its the code related to reading from the MongoDb server.

See the backend cloud function code related to Mongo below:

const collection = Parse.Object.extend("Collections");
			const query = new Parse.Query(collection);
			query.equalTo(
				"contractAddress",
				requestContract.contractAddress
			);
			const collectionResult = await query.first({
				useMasterKey: true,
			});

			if (collectionResult) {
}

(obviously, there is a return etc with a bunch of logic statements etc)

did you add logging for the parameters

you can use console.log(request) to see all the parameters, request.user is not present for authenticated users?

you can also connect directly to mongo db in cloud code if needed and make the request directly that way without using Parse sdk

Hey! Did someone managed to sucessful integrate this with React Native?
My iOS app is working fine with cloud, but doing self host i’m not able to connect with any provider (like metamask).

I’ve followed the migration links but doesn’t seem to match properly the RN project.
It seems to fail every time when trying to use the walletconnect connet / auth.
Any help?

Thanks.

You can make a new topic with more details.

Done! Here is the link:

Thanks!

1 Like

Hello,

The sample Parse Server is still using Moralis.Auth service, and it needs Moralis API Key. How can I change this to a Moralis-independent code


Parse.Cloud.define('requestMessage', async ({ params }: any) => {
  const { address, chain, networkType } = params;

  const message = await requestMessage({
    address,
    chain,
    networkType,
  });

  return { message };
});

You have to change more than that code if you want to make it independent. There is also a function that validates the signature.

You can try by returning your custom message. Make sure that you take in consideration possible security issues. There is an EIP that describes that authentication.

1 Like

Hi.there. is there anyway to combine same address from using react-moralis authenticate and using signMessageAsync function?
I used same address to authenticate host parse-server, but got two different objectId.
Here’s my code
1.use react-moralis pacakge

import { useMoralis } from "react-moralis";

  const { authenticate, auth } = useMoralis();

   const { message } = await Moralis.Cloud.run("requestMessage", {
        address: account,
        // chain: parseInt(chain.id!, 16),
        chain: chain.id!,
        networkType: "evm",
      });
      let result = await authenticate({ signingMessage: message });

the result in mongo
image

and other

  const verifyMessage = (message, signature, networkType) =>
    apiPost({
      endpoint: "auth/sign-message",
      params: {
        message,
        signature,
        networkType,
      },
    });

    const signature = await signMessageAsync({ message });
    const { user } = await verifyMessage(message, signature, "evm");

Thanks for reply

What is the difference in backend between these two cases?
Like what code in backend it is called/used in these cases