[SOLVED] isAuthenticated React problem

Hi everyone, Iโ€™m having problems with useMoralis and React. Let me explain, I have a site with several web pages (built with react), the first page is the login page, before you can visit the others you need to be authenticated. To do this I use react-moralis and the useMoralis hook. So far so good, in each of the other pages there is a component (navbar) that checks if the user is authenticated through useEffect () and if the user is not authenticated it sends me back to login. The problem is that every time I refresh a page, isAuthenticated becomes false, I get sent back on login, isAuthenticated returns true and I am brought back to the homepage โ€ฆ does anyone have a solution?

Code in my component Navbar =>
const { authenticate, isAuthenticated, isAuthenticating, user, account, logout } = useMoralis();
useEffect(()=> {

    if(!isAuthenticated) router.push("/login");


Oh, if I browse to different pages this problem doesnโ€™t happen, only on the refresh

Maybe you have to add some other dependency to check if it is loading

Iโ€™ve installed all the dependencies

You can try this way

const { isInitialized, isAuthenticated } = useMoralis();
  useEffect(() => {
    const checkUser = () => (!isAuthenticated ? router.push("/login") : null);
    isInitialized && checkUser();
  }, [isInitialized, isAuthenticated]);

It works! Really thank you!

1 Like