[SOLVED] Auth error code":101 Error: Moralis auth failed, invalid data

I comment that I already authenticate with metamask, but if I restart the page I must restart the localhost to authenticate again via metamask, if I don’t it gives me this status code: error: Moralis auth failed, invalid data {"code":101 ,"stack": "Error: Moralis auth failed, invalid data\n at C:\\Users\\david zuccarini\\Documents\\Programming\\react\\Koolinart\\backend\\src\\auth\\ MoralisEthAdapter.ts:32: 13\n in processTicksAndRejections(node:internal/process/task_queues:95:5)\n in async Promise.all (index 0)"} , and after restarting the server, if it authenticates normally, again. this is because?

Did you have any sessions added? Coz otherwise Metamask will not recognized whether you have signed in or not

This is the problem:

I share in drive because the .gif is not uploaded:

as you can see in the video. I have to restart the server so that it picks up the login again. If I log in, and wait a while. when refreshing the page. It gives me the error in red from the console.log. but if i stop the server and run it again. log in normally.

and in production give me the same error.

Hi @davidzuccarini

It seems strange. Moralis auth failed, invalid data can occur when the message or signature which you are passing to the authentication is null or undefined. You can try adding some loggers to debug more about this.

Do you have a production URL where I can test authentication?

Hi john, yes of course www.koolinart.com. thanks u.

Hi @davidzuccarini

do you know which part of the code is throwing this error? I am seeing this in the browser console.

TypeError: Cannot read properties of undefined (reading 'get') 

And more thing to check is why the issued time is greater than the expiry time. The expiry time should be greater than the issued time. This could be the reason why auth is failing.
image

Well, if I know what part of the code it is, it’s the part where the user reads after authenticating, for that reason it doesn’t read the get.

the part you mention about the date is this (it’s in the backend):

import Moralis from 'moralis';

import { authRequests } from '../store';

import { ParseServerRequest } from '../utils/ParseServerRequest';

import config from '../config';

const serverRequest = new ParseServerRequest();

interface ParseUser {

  objectId: string;

}

export interface RequestMessage {

  address: string;

  chain: string;

  network: string;

}

const DOMAIN = 'koolinart.com';

const STATEMENT = 'Welcome to Koolinart, Please sign this message to confirm your identity.';

const EXPIRATION_TIME = 900000;

const TIMEOUT = 15;

const url = new URL(config.SERVER_URL_AUTH);

const urltoString = url?.toString()

const now = new Date();

const expirationTime = new Date(now.getTime() + EXPIRATION_TIME);

export async function requestMessage({

  address,

  chain,

  networkType,

}: {

  address: string;

  chain?: string;

  networkType: 'evm' | 'solana';

}) {

  if (networkType === 'evm' && chain) {

    return requestMessageEvm({ address, chain, networkType });

  }

  if (networkType === 'solana') {

    return requestMessageSol({ address, networkType });

  }

  throw new Error(`Invalid network: ${networkType}`);

}

async function requestMessageEvm({

  address,

  chain,

  networkType,

}: {

  address: string;

  chain: string;

  networkType: 'evm';

}) {

  const result = await Moralis.Auth.requestMessage({

    address,

    chain,

    networkType,

    domain: url.hostname,

    uri: urltoString,

    statement: STATEMENT,

    notBefore: now?.toISOString(),

    expirationTime: expirationTime?.toISOString(),

    timeout: TIMEOUT,

  });

  const { message, id, profileId } = result.toJSON();

  authRequests.set(message, { id, profileId });

  return message;

}

async function requestMessageSol({ address, networkType }: { address: string; networkType: 'solana' }) {

  const result = await Moralis.Auth.requestMessage({

    address,

    networkType,

    network: 'devnet',

    domain: DOMAIN,

    statement: STATEMENT,

    uri: urltoString,

    expirationTime: expirationTime?.toISOString(),

    timeout: TIMEOUT,

  });

  const { message, id, profileId } = result.toJSON();

  authRequests.set(message, { id, profileId });

  return message;

}

export interface VerifyMessage {

  network: string;

  signature: string;

  message: string;

}

export async function verifyMessage({ network, signature, message }: VerifyMessage) {

  const storedData = authRequests.get(message);

  if (!storedData) {

    throw new Error('Invalid message');

  }

  const { id: storedId, profileId: storedProfileId } = storedData;

  const authData = {

    id: storedProfileId,

    authId: storedId,

    message,

    signature,

    network,

  };

  // Authenticate

  const user = await serverRequest.post<ParseUser>({

    endpoint: `/users`,

    params: {

      authData: {

        moralis: authData,

      },

    },

    useMasterKey: true,

  });

  // Update user moralisProfile column

  await serverRequest.put({

    endpoint: `/users/${user.objectId}`,

    params: {

      moralisProfileId: storedProfileId,

    },

    useMasterKey: true,

  });

  // Get authenticated user

  const updatedUser = await serverRequest.get({

    endpoint: `/users/${user.objectId}`,

    useMasterKey: true,

  });

  return updatedUser;

}

hey but look at this, when I reset the server. for the first time if you log in and look at the date.
then after a while the date thing happens.

2023-07-01_04h22_51

If you authenticate at this time, restart the server. yes authenticate after a while stop doing it.

sorry if my english sounds bad

I think I already saw the error, hehe what a newbie xD

Of course, for some reason I took out those variables and declared them outside the function. and variables of type const cannot be modified after they are ‘‘globally’’ stored. and had not seen it. sorry guys and thanks for your patience and help always.

sorry for the question john it has nothing to do with this. i have recommended a lot of moralis, and i need it for other projects and this same project, i need to integrate this to unreal, once i asked something about unreal in another thread but i didn’t get an answer. but is there a way to authenticate parse server and parse server data bring them to unreal ? the authentication. I have several projects about this. I would be grateful. If you could guide me the route to carry it out.

So the auth error is fixed now? :sweat_smile:

I remember about your old post about unreal engine, I don’t have any experience with the unreal environment so I cant suggest much.

For the authenticate the process flow will be the same.
You will have the backend server with requestMessage and verify signature endpoints. 1st we need to requestMessage and send it to the unreal client, where will need to sign the signature using a wallet in the unreal client app.

To sign a signature or to interact with the wallet requires using some wallet connectors. (I am not aware if there is any package to support this).
Once the signature is signed we send it to the backend for verification using the verify signature function.

This auth workflow is the same for all apps.

hahaha yes what a shame, hahaha it happened to me newbie xD sorry. I have already authenticated several times and everything seems to work fine. moving forward with the project and all good. I go with the streams. to test the streams. and I will comment. I think I have a record in moralis,

theonethatasksthemost.com xD.

okay, if there is an unreal sdk called emergence I should investigate and study a little more I’ll try it with this to see if it turns out I’ll comment, I’d be willing to record for the community.

I am always grateful in advance for the help and the moralis community. Thanks for the time. Hugs and greetings.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.