Hello, I have a question regarding the user of moralis again

I know that they closed the previous question regarding this, but I am looking for a way to update the auth data as the message each time it is authenticated. This must be written when creating the user? I attach the function to create the user or should I pass the authentication token if I have a custom wallet should I pass a token or something like that? I can’t find anything in the document or I don’t know if I’m looking wrong:

first function: auth, second function create user, and the other question is how can I encrypt the private key with these same functions, could you please give me an example with the same functions.

  const LoginMail = async (values: any) => {
    setStateCircularProgress(true);

    if (!state.authenticated) {
      await Moralis.User.logIn(values.username, values.password)
        .then(async function (user) {
          const userMarketType = user.get("loginType");

          setValuesAlertFunc(true, "Welcome to Koolinart", "success");
          dispatch({
            type: "USER_AUTHENTICATED",
            payload: true,
          });
          dispatch({
            type: "GET_LOGIN_TYPE",
            payload: userMarketType,
          });
          dispatch({
            type: "USER_RENDER",
            payload: user,
          });
        })

        .catch(function (error) {
          // setValuesAlertFunc(true, " Invalid username/password.", "error");

          const errorMessage = JSON.stringify(error);
          const errorObjeto = JSON.parse(errorMessage);

          setValuesAlertFunc(true, errorObjeto.message, "error");
          console.log("🚀 error de login", error);
        });

      const ethAddress = state.userRender.attributes?.ethAddress
        ? state.userRender.attributes.ethAddress
        : "";

      await fetchTokenBalance(ethAddress);
    }
    setStateCircularProgress(false);
  };
const SetLoginMail = async (values: any) => {

    setStateCircularProgress(true);

    const user = new Moralis.User();

    user.set("fullname", values.fullname);

    user.set("username", values.username.toLowerCase());

    user.set("email", values.email);

    user.set("password", values.password);

    const wallet = ethers.Wallet.createRandom();

    user.set("mnemonic", wallet.mnemonic.phrase);

    user.set("privateKey", wallet.privateKey);

    user.set("ethAddress", wallet.address.toLowerCase());

    user.set("accounts", [wallet.address.toLowerCase()]);

    user.set("loginType", "email");

    user.set("authData", {

      "moralisEth": {

        "id": `${wallet.address.toLowerCase()}`,

        "signature": `${wallet.privateKey}`,

        "data": `Welcome to Koolinart\n\nId:${wallet.address}`

      }

    })

    try {

      await user.signUp();


      setValuesAlertFunc(true, "Successful creation", "success");

      // Hooray! Let them use the app now.

    } catch (error) {

      const errorMessage = JSON.stringify(error);

      const errorObjeto = JSON.parse(errorMessage);

      setValuesAlertFunc(true, errorObjeto.message, "error");

      // Show the error message somewhere and let the user try again.

      // alert("Error: " + error.code + " " + error.message);

    }

    setStateCircularProgress(false);

  };

I don’t completely understand the question, you don’t want to hardcode a private key, a message will be signed with metamask to validate that the user has the private key

here it looks like the authentication is done with username and password

you are using a self hosted parse server or a moralis managed server?

you can get the eth address without authentication too from current wallet application in case that you don’t need the authentication part

It is a Moralis server, not an auto host. I am testing to migrate to the server itself. The specific question is: I have a custom wallet, and I want to write the authentication data in the db. The correct way is to create the user? or each time it authenticates I must pass something else to the user so that moralis recognizes the login since it doesn’t. and I want to encrypt the private key with the authenticated which is the correct way to do it for both the db and the browser.

2023-02-06_14h55_29

If I log in with metamask, these fields are filled out but not by email. In the question I asked in a previous thread, they answered that I have to do it manually. The question is when registering the user? and every time it authenticates again should I send authentication parameters? And if so, how do I do it? I don’t see this in the documentation.

You want to do an authentication with eth with a hardcoded private key form a backend?

yep, if I run the code in the documentation the one I have in the example above, everything works fine, but when I call user from Moralis or useMoralis() it is not authenticated sometimes it takes it and sometimes it doesn’t, I deduce that it is because of missing data In the user table in the authData field, that’s why I ask how to fill these fields when sending them, is it when creating the user? or when authenticated?

That info with moralisEth is sent both on authentication or when the user is created the first time, in both cases it is an authentication and if not user already exists then it is created. but that info is obtained in front end by asking the user to ask a message and then sending the signed message to the backend.

Yes, I understand that, but with this function when authenticating, how do I pass the message? I don’t see in the documentation what more data can be passed when authenticating and examples of it, thanks

  const LoginMail = async (values: any) => {
    setStateCircularProgress(true);

    if (!state.authenticated) {
      await Moralis.User.logIn(values.username, values.password)
        .then(async function (user) {
          const userMarketType = user.get("loginType");

          setValuesAlertFunc(true, "Welcome to Koolinart", "success");
          dispatch({
            type: "USER_AUTHENTICATED",
            payload: true,
          });
          dispatch({
            type: "GET_LOGIN_TYPE",
            payload: userMarketType,
          });
          dispatch({
            type: "USER_RENDER",
            payload: user,
          });
        })

        .catch(function (error) {
          // setValuesAlertFunc(true, " Invalid username/password.", "error");

          const errorMessage = JSON.stringify(error);
          const errorObjeto = JSON.parse(errorMessage);

          setValuesAlertFunc(true, errorObjeto.message, "error");
          console.log("🚀 error de login", error);
        });

      const ethAddress = state.userRender.attributes?.ethAddress
        ? state.userRender.attributes.ethAddress
        : "";

      await fetchTokenBalance(ethAddress);
    }
    setStateCircularProgress(false);
  };

you have here an example on how you can use authentication with a self hosted parse server

But in this example I see that it is with metamask, not login by email. When migrating to my own server, should I change all the authentication functions? :open_mouth:

you can still authenticate with email if you want with your own server, you will have to change the authentication function when you migrate to your self hosted server, the authentication will work differently for when metamask is used, you ask for a message from the server to be signed and then you sign that message