Request.user undefined in Cloud Function

We are setting up a Parse server following authentication explained here:

When we run a cloud function from the frontend, with the requireUser: true on the cloud code, we get an error, and logging the request we see that the user is ā€œundefinedā€.

Strange thing is that the user is correctly authenticated on the database, and we get the session.

How we can setup a Parse User in the authentication process, to have a request.user populated with the right user running the request?

Thanks

You can try to look at the request that is sent in the browser network tab. It should be able to identify the user from the session token. Alternatively you could check the session token in the cloud function as an alternative.

The issue is that even if we have _User and _Session populated, the request.user is undefined on cloud, so if we try to get from request.user the getSessionToken, we get an error

you can print the entire request object to see what information you have available

yes exactly, is what I did, and Iā€™ve discovered user is ā€œundefinedā€, even if is apparently logged in, on db and client

no session token in that request? are you sure the the session token is sent from the browser?

I donā€™t see a session token in the request, I think should be in ā€œuserā€ that is undefined.

How can be sent, any reference on how it could be done?

Thanks

it should be sent automatically by the SDK, you can send it any other way and check it then in the cloud code ti see if the session toke is valid, I donā€™t remember now the exact variable name for the session token that should be used in the request

the problem is that if I have the user as undefined, I canā€™t compare the information, and at the same time, Iā€™ve other issues calling for example request.user.get(ā€œethAddressā€)

I would have liked to know why the Parse User is undefined nevertheless the db is populatedā€¦ I used this part that should do the trick from your guide:

// Authenticate
  const user = await serverRequest.post<ParseUser>({
    endpoint: `/users`,
    params: {
      authData: {
        moralis: authData,
      },
    },
    useMasterKey: true,
  });

that part is from backend sent? I see useMasterKey there

I thought that you are using the browser to authenticate

Yes is from the backend, Iā€™m following step by step this guide:

and added wagmi to authenticate with the wallet.

Once I do this, I have the DB populated (_User and _Session), but the issue Iā€™ve explained for the request.user

In backend it is a little different, you donā€™t have to authenticate from backend, if you know in backend the session token then you can validate it directly in the database

I thought this was something that directly was done by Parse (same way is populating _User and _Session).

Is there any example about this part you are explaining? For example, how can I know who is the user calling the cloud function if I donā€™t have information on request.user?

can you give more info about the context of what you are trying to do?

Sure, Iā€™m authenticating the user, following the link of the Parse server Auth, and the _User is correctly written in the DB together with _Session.

Iā€™m using next_auth also.

Then, Iā€™m trying to call from the frontend a cloud function on Parse, with this


  Parse.initialize(process.env.NEXT_PUBLIC_MORALIS_APP_ID);
  Parse.serverURL = process.env.NEXT_PUBLIC_SERVER_URL;
  const params = { nftId: _nftId };
  const elementById = await Parse.Cloud.run("getElementById", params);

In the cloud code, Iā€™ve requireUser: true

The problem is that the request.user arrives with undefined

You can try to use Moralis SDK in frontend instead of Parse SDK, Moralis SDK may check the session token in local storage

This would be Moralis v1? Iā€™m trying to migrate to Moralis v2

yes, that would be moralis v1 sdk in front end only, not in backend

I would like to keep the v2 for authenticationā€¦ Iā€™ve done all this work so far to use that without the v1, also for the issues unresolved related to walletconnect.

Iā€™ve seen that if I do an explicit logIn with Parse before calling a cloud function, the request.user is not unfedined.

But the logIn from Parse needs a username and password, and my purpose was to keep the only authentication option with a wallet as it was in my previous codebase with Moralis.

Is there any possibility to do a kind of a ā€œmockā€ login keeping it secure with the only wallet, as maybe was done in Moralis v1?

you can try to explicitly send the session token in that request to the cloud function, you donā€™t have to send the auth data, only the session token, you can check where parse saves the session token and send it (I would think that parse sdk should do this by default)