0

Every time I refresh the page when I am using a component within the Private Route below, I navigate to the /login. Is there a way to prevent that from happening? I only want to navigate to login if there is no currentUser.

const PrivateRoute = ({ children }) => {
  const { currentUser } = useContext(AuthContext);
  return currentUser ? children : <Navigate to="/login" />;
};

  • 1
    I'm guessing currentUser starts as null/undefined, then shortly after gets loaded, perhaps via `onAuthStateChanged`? If so, i recommend you add a loading state to the provider which starts as true, and have privateRoute render null if loading is true. If not, show us the code where you do the AuthContext.Provider. – Nicholas Tower Jan 03 '23 at 16:01

0 Answers0