User is null after signing in

Hi! I’m trying to get the user’s data after signing in.

But this doesn’t seem to work.

await authenticate({ signingMessage: "Welcome!", onComplete: () => console.log("user", user) })

If you are using react-moralis, try using user from useMoralis hook to get the user data after authentication is successful.

const { user } = useMoralis();

useEffect(() => {
    user && console.log(user);
  }, [user]);

I’m trying to get the user’s info asap because I am planning to use it for a query.

const { fetch } = useMoralisQuery(

    "userLikes",

    (query) => query.equalTo("user", user),

    [],

    { autoFetch: true }

);

However, whenever I try to use fetch, it’s giving me null at first.

change this autoFetch value to false and use useEffect to call fetch function. So the fetch will be executed as soon as the user is loggedIn.

useEffect(() => {
    user && fetch()
  }, [user]);

Still giving me a null value. Even if the user already loggedin, I’m trying to fetch what the user liked but giving me a null. However, if I manually fetch it using a button, it works properly.

user is still null after logging it in a useEffect like johnversus posted? Check that you have a user item saved in your app’s localStorage (check with developer tools). Or are you saying the data from the query hook is null?

However, if I manually fetch it using a button, it works properly.

Can you post this code.

You should also add user to the dependency of the hook here having

(query) => query.equalTo("user", user),
  [user],