Authentication Function returning undefined user

I am using the ‘authenticate’ function from the useMoralis() hook. But it gives me an undefined user in the response. Usually it returns the user object, but it is not for some reason. Can someone tell me how to fix this? Thank you.

Can you show how you’re doing it ?

const login = async () => {
    if (!isAuthenticated) {
      setWalletMessage('Connecting...');
      console.log(authenticate)
      await authenticate({ signingMessage: 'Kaiju Kingz Login' })
        .then(async function (user) {
          console.log(user)
          if (user) {
            console.log('logged in user:', user);
            let userAddy = user.get('ethAddress');
            console.log(userAddy)
            setWalletMessage('0x...' + userAddy.slice(-4));
            setMoralisUser(user);
          } else {
            setWalletMessage('');
            console.error('Wallet Connection Failed 😒');
          }
        })
        .catch(function (error) {
          setWalletMessage('');
          console.error('Wallet Connection Failed', error);
        });
    }
  };

Here is the code for the authentication. After ‘await authenticate’ the user in the .then is undefined.

when authenticating, it is creating undefined users in the database. why is this happening?

You can refactor your code to either use async await or .then on onSuccess and onError.
Here’s an example

const login = async () => {
  if (!isAuthenticated) {
    try {
      setWalletMessage("Connecting...");

      let user = await authenticate({ signingMessage: "Kaiju Kingz Login" });
      console.log(user);

      if (user) {
        console.log("Logged in user:", user);
        let userAddy = user.get("ethAddress");
        console.log(userAddy);

        setWalletMessage("0x..." + userAddy.slice(-4));
        setMoralisUser(user);
      } else {
        setWalletMessage("");
        console.error("Wallet Connection Failed 😒");
      }
    } catch (error) {
      setWalletMessage("");
      console.error("Wallet Connection Failed", error);
    }
  } else {
    console.log('User already Authenticated')
  }
};

I have refactored in this way and it still creates an empty user in the _User table. It also returns undefined, so user = undefined.

Any specific error message in server logs?
What is the server url?

This is the server url: https://cjvznggxu4qw.usemoralis.com:2053/server How can I check the logs? I am also getting an invalid session token on my CMS site that doesn’t even require authentication. This error also makes anything I fetch from the database undefined.

Also, these issues began after I deleted all of the users in the table. I probably should have mentioned that

that makes some sense, when you delete users from table you have to also remove the corresponding addresses from EthAddress table

Thank you, this allowed me to authenticate. Do you know why I am getting invalid session token now? My fetches to the database are undefined. I attempted to fix this by removing all rows in the _Session table but that didn’t work.

when do you get an invalid session token error? you should get a session token after you authenticate, that session token should be saved in db in that _Session table

I don’t really know. I get it as soon as I load the website. On my CMS site there is no authentication yet I still get the error. I also get it on the main site which requires authentication as soon as I load the page.

there could be a session already present in local storage, try to logout