Self-hosting your Moralis server

Not sure where you mean to change the default timeout interval? In the parse server or in the frontend app code?

In cloud code in parse server in the function that gets the message to be signed

Oh right, I see. I changed to 90 but same error. I think its due to my frontend code which comes from an old Moralis eth boilerplate. I’ve tried the demo frontend app and it seems to work (login etc) and I can see the authentication code is different. I’m also logging in via a polygon/matic wallet but doubt that will make a difference. I fiddled around with the frontend code (copied over from the parse frontend demo code related to auth) and it worked (kinda).

But then I get this:

xhr.js:212 
 POST http://localhost:1337/server/functions/getNFTs 403 (Forbidden)

When trying to get NFT data for that logged in User. Also console.log of account is null so something still not quite correct.

I’m getting a MongoTopologyClosedError whenever I try to use the code from the video to migrate from Moralis Hosted MongoDB to self hosted.

The other MongoClient that connects to my self hosted server connects just fine.

How can I connect to the my Moralis MongoDB to get the data off with the ip address and port provided?

MongoTopologyClosedError: Topology is closed
    at processWaitQueue (S:\Dropbox\QuarkStars\mongodb-migration\mongoDBmigartion\node_modules\mongodb\lib\sdam\topology.js:537:46)
    at Topology.selectServer (S:\Dropbox\QuarkStars\mongodb-migration\mongoDBmigartion\node_modules\mongodb\lib\sdam\topology.js:298:9)
    at S:\Dropbox\QuarkStars\mongodb-migration\mongoDBmigartion\node_modules\mongodb\lib\operations\execute_operation.js:32:29
    at maybePromise (S:\Dropbox\QuarkStars\mongodb-migration\mongoDBmigartion\node_modules\mongodb\lib\utils.js:357:5)     
    at executeOperation (S:\Dropbox\QuarkStars\mongodb-migration\mongoDBmigartion\node_modules\mongodb\lib\operations\execute_operation.js:16:37)
    at FindCursor._initialize (S:\Dropbox\QuarkStars\mongodb-migration\mongoDBmigartion\node_modules\mongodb\lib\cursor\find_cursor.js:54:50)
    at FindCursor.[kInit] (S:\Dropbox\QuarkStars\mongodb-migration\mongoDBmigartion\node_modules\mongodb\lib\cursor\abstract_cursor.js:428:14)
    at next (S:\Dropbox\QuarkStars\mongodb-migration\mongoDBmigartion\node_modules\mongodb\lib\cursor\abstract_cursor.js:499:22)
    at fetchDocs (S:\Dropbox\QuarkStars\mongodb-migration\mongoDBmigartion\node_modules\mongodb\lib\cursor\abstract_cursor.js:256:17)
  [Symbol(errorLabels)]: Set(0) {}
}
PS S:\Dropbox\QuarkStars\mongodb-migration\mongoDBmigartion> node index
MongoServerSelectionError: connection <monitor> to 159.223.161.245:56728 closed
    at Timeout._onTimeout (S:\Dropbox\QuarkStars\mongodb-migration\mongoDBmigartion\node_modules\mongodb\lib\sdam\topology.js:293:38)
    at listOnTimeout (internal/timers.js:557:17)
    at processTimers (internal/timers.js:500:7) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) { '159.223.161.245:56728' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: null,
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined,
  [Symbol(errorLabels)]: Set(0) {}
}
1 Like

But wait would this even migrate users. I assume their data is not so eaily accessible by knowing the ip address.

Which version of moralis-v1 / react-moralis are you using? Make sure you are using the react-moralis versions of enableWeb3() and authenticate() from the useMoralis hook in your new authentication code - not Moralis.enableWeb3() or Moralis.authenticate().

There was a similar issue here which has been fixed in the latest moralis-v1.

If you cannot update to these newer versions e.g. from moralis@1x without it breaking that old boilerplate, you can try this workaround here where you update your Parse Server instead.

Make sure you have whitelisted your public IP address in your Moralis server’s settings. You can use tools like MongoDB Compass or mongodump using the IP address and port given in the Moralis dashboard.

With mongodump and mongorestore, you can migrate all data including user data.

Netlify or Vercel aren’t really suitable for normal Node.js backend/server deployments. For a free option you can look at a provider like Render.

I see that they updated the documentation to show how to deploy in aws: https://docs.moralis.io/docs/deploy-to-production#setup-ecs-environment

This documentation is incomplete, could someone tell me what are the steps to follow to implement the server in aws?

Can you outline how it is incomplete please?

In the last section it shows how to create a service, it still remains to show how to add the server code

Hey glad. Thanks for the response. I’ll check it out over the weekend.

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}