[SOLVED] Trying to set up auth API for new self hosted server

There is absolutely nothing triggered in the network tab when I run the function.

you have to click on the button after you open the network tab

Yes, after the page loads I go to the network tab and clear it to get rid of the unrelated stuff that happens on initial load.
Then I click the button, triggering the function
nothing happens.
That screenshot was taken after clicking the button.

also nothing in console log?
can you try with a simpler cloud function (like getServerTime)? and to call it directly with a postman? (you may have to use POST)

I’m not very familiar with making api calls in react, so I may have done it wrong.

const result = await fetch(
        "http://localhost:1337/server/functions/getServerTime",
        { method: "POST" },
      );
      console.log(result);

results in a 403 error

Can you post your package.json?

console.log(“message set”);

You aren’t getting this logged? Can you log error from the try catch.

catch (error) {
  console.log("err", error);

package.json:

{
  "name": "gigautility",
  "version": "0.2.0",
  "dependencies": {
    "@ant-design/icons": "^4.7.0",
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "@walletconnect/web3-provider": "^1.6.6",
    "@web3auth/web3auth": "^2.1.3",
    "antd": "^4.16.13",
    "assert": "^2.0.0",
    "crypto-browserify": "^3.12.0",
    "https-browserify": "^1.0.0",
    "magic-sdk": "7.0.0",
    "moralis": "^1.2.3",
    "moralis-v1": "^1.11.0",
    "os-browserify": "^0.3.0",
    "react": "^17.0.2",
    "react-app-rewired": "^2.2.1",
    "react-blockies": "^1.4.1",
    "react-dom": "^17.0.2",
    "react-moralis": "^1.2.1",
    "react-query": "^3.39.1",
    "react-router": "^5.2.1",
    "react-router-dom": "^5.3.0",
    "react-scripts": "^5.0.0",
    "stream-browserify": "^3.0.0",
    "stream-http": "^3.2.0",
    "url": "^0.11.0",
    "web-vitals": "^1.0.1",
    "yarn": "^1.22.17"
  },
  "scripts": {
    "start": "set \"GENERATE_SOURCEMAP=false\" && react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-app-rewired eject",
    "devchain": "node Truffle/scripts/devChain.js",
    "connect": "moralis-admin-cli connect-local-devchain",
    "watch:events": "moralis-admin-cli add-contract",
    "deploy": "node Truffle/scripts/deployContract.js",
    "deploypage": "gh-pages -d build",
    "clean": "npx gh-pages-clean",
    "lint:check": "eslint .",
    "lint:fix": "eslint --fix",
    "prettier:check": "prettier --check .",
    "prettier:fix": "prettier --write \"**/*.{js,jsx,ts,tsx,css,md,json,html}\" .prettierrc --config ./.prettierrc",
    "format": "npm run lint:fix && npm run prettier:fix",
    "prepare": "husky install"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "eslint": "^7.11.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "eslint-plugin-react": "^7.28.0",
    "eslint-plugin-react-hooks": "^4.3.0",
    "gh-pages": "^3.2.3",
    "husky": "^7.0.0",
    "lint": "^0.7.0",
    "prettier": "^2.5.1"
  }
}

error: err Error: You need to call Moralis.start with an applicationId before using Moralis.

Moralis is initialized in the app file, the connect button is in the header bar of the app file

or rather, Moralis is initiated in the index.js file, and isInitialized is checked in the app file

Can you try installing these versions (which are used in the migration client demo):

[email protected]
[email protected]

And remove [email protected].

That definitely did something. The request is going out now, and metamask is prompting for a message signing, but I’m getting this new error

err Error: Moralis auth failed, invalid data
at handleError (RESTController.js:438:1)

Is your Parse server code the same as the demo or have you made any changes?

Can you also your Parse server’s package.json.

server code should be the same, I dont recall making any changes

{
  "name": "demo-parse-server-migration",
  "version": "1.0.0",
  "main": "dist/index.js",
  "private": true,
  "dependencies": {
    "@codemirror/language": "^0.20.0",
    "@moralisweb3/core": "^2.2.0",
    "@types/node": "^18.7.15",
    "dotenv": "^16.0.1",
    "envalid": "7.3.1",
    "express": "^4.18.1",
    "express-rate-limit": "^6.5.1",
    "graphql": "^16.6.0",
    "graphql-ws": "^5.10.1",
    "moralis": "^2.2.0",
    "parse-server": "^5.2.5"
  },
  "devDependencies": {
    "@moralisweb3/eslint-config": "^1.0.0",
    "@typescript-eslint/eslint-plugin": "^5.12.0",
    "@typescript-eslint/parser": "^5.12.0",
    "axios": "^0.27.2",
    "eslint": "^8.9.0",
    "eslint-plugin-import": "^2.26.0",
    "mongodb-runner": "^4.9.0",
    "prettier": "^2.7.1",
    "ts-node": "^10.9.1",
    "typescript": "^4.7.4"
  },
  "scripts": {
    "dev": "ts-node src/index.ts",
    "start": "node build/index.js",
    "build": "tsc",
    "lint": "eslint --ext .js,.ts .",
    "format": "prettier --write 'src/**/*.{js,ts}'",
    "dev:db-start": "mongodb-runner start",
    "dev:db-stop": "mongodb-runner stop",
    "gen:cloud": "ts-node ./scripts/generateCloudCode.ts",
    "tsc": "tsc"
  }
}

Can you doublecheck both .env files for your Parse server and React app and that they more or less match (except for exact API key, Mongo/redis, etc.).

I dont recall making any changes

Can you post your Parse server’s cloud/main.ts code. You aren’t getting any other errors in your browser console or both server terminals?

the server is getting the message request:
info: Ran cloud function requestMessage for user undefined with:
Input: {“address”:“0xb03139137d06db446faa490d8a6f07d411f849c0”,“chain”:1,“network”:“evm”}

.env both look good

the only errors I’m getting now is the invalid data one.

main.ts:

/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-var-requires */
declare const Parse: any;
import './generated/evmApi';
import './generated/solApi';
import { requestMessage } from '../auth/authService';

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

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

  return { message };
});

Parse.Cloud.define('getPluginSpecs', () => {
  // Not implemented, only excists to remove client-side errors when using the moralis-v1 package
  return [];
});

Parse.Cloud.define('getServerTime', () => {
  // Not implemented, only excists to remove client-side errors when using the moralis-v1 package
  return null;
});

What happens if you run npm run build for the Parse server?

same results as if I use yarn build

It looks like the message request is returning 200, but the invalid data is coming from the auth call

the sign request only says “Moralis Authentication” and not the message written in auth/authservice.ts
I went in and changed the domain and uri in that file to localhost and rebuilt, but I’m still getting the same error

Can you try authenticating with a clone of the migration client - you just need to copy over your .env file. Ideally you do the same with the parse server.

It would be easier to find out where the issue is starting from working baseline projects so we don’t have to make any assumptions about how you’ve set up your own server and React client.

For example, your current Parse server dependencies expects networkType, not network in requestMessage. On my end if I try using network, I get type errors when trying to build.

So, this set off a whole slew of errors that should not exist and I dont understand.

First, I cloned the migration react client and ran it using my current server. everything ran smoothly, I signed the correct message with the appropriate data, but then I got this error

image

remembering an issue I had a while ago, I deleted the entry for my wallet in the __ethAddress table in my mongoDB. That had no effect.

Then, I created a fresh server and copied over my .env file.
ran yarn install
ran yarn build

error, saying the cors module was not found
it existed, so I ran npm i --save-dev @types/cors (which is what the terminal suggested.
that fixed it, I was able to run yarn build

then I tried running yarn dev to start the server
error, the module ‘ethers’ is not found
the fix for cors did not fix this
I tried manually running yarn add ethers
error, Host key verification failed.
fatal: Could not read from remote repository.

I tried copying my package.json from my current server to the new one and ran yarn install again but kept running into the same issue

For the auth is already used error you can delete the _User object for the wallet address you’re using from your MongoDB database. Can you see if that will work with your current custom Parse server?

I tried copying my package.json from my current server to the new one and ran yarn install again but kept running into the same issue

This would defeat of purpose of trying to get the second (demo) server working as it is by default.

I got your same errors with the latest parse-server-migration, but installing both @types/cors and ethers as you mentioned fixed these on my end - I did not get the host key verification failed issue. Try deleting yarn.lock and node_modules and installing everything again.

I must have gotten the code for my server while you guys were still switching it around. I had no issue installing ethers today, and I got the authentication working on the react client from both the fresh server install and from my server. There was an issue where I had to change “networkType” to “network” in the migration react client (it wouldnt let me change it on my server and I didnt want to mess with it)

However, I’m still getting errors when trying to connect my actual website.
the message request runs fine, I get the “message set” console log and I can see the api call go out and come back successfully.

the message that I sign looks like this

then the users api call fails. console shows a 404 error, network tab looks like this

console on the server logs this

error: Moralis auth failed, invalid data {"code":101,"stack":"Error: Moralis auth failed, invalid data\n    at C:\\Users\\cavan\\Documents\\code\\Moralis-Server\\src\\auth\\MoralisEthAdapter.ts:32:13\n    at processTicksAndRejections (node:internal/process/task_queues:96:5)\n    at async Promise.all (index 0)"}

I think there is some error with the actual returning of the message to the front end, since the correct message does not show up to be signed. But I’m not sure where that might be, since I was able to get my server to work correctly with the migration demo front end.

In the tutorial there is a specific code for authentication that is different than the code that was working with a Moralis server. You used that code?