0

I have a function that redirects my user once they enter the page so they don't receive a not found, it is executed on an useEffect. My problem is that after the first load of the page I don't want the user to be redirected anymore and they should refresh and stay in the same page, how can I tell the difference if the user is entering the page or refreshing it?

const onAppLoad = useCallback(async () => {
  Auth.currentSession()
    .then(response => {
      authRef.current = response;
      setUser(response.idToken.payload);
      navigate('/employees');
      setIsAuthenticated(true);
    })
    .catch(() => {
      navigate('/login');
    })
    .finally(() => {
      setFetching(false);
    });
}, []);
Drew Reese
  • 165,259
  • 14
  • 153
  • 181
TurtleTail
  • 163
  • 6
  • 1
    *redirects my user once they enter the page so they don't receive a not found* - why would they receive `not found`? – Konrad Oct 14 '22 at 20:13
  • @KonradLinkowski because of how the code was configurated, I can't change it and need to work around it. – TurtleTail Oct 14 '22 at 20:15
  • 1
    Because how *what* code was configured? Sounds like we need more details/information/context/requirements around your use case. What exactly does "first load" even mean for example? Like the first time a user accesses the app & route, ever? What is considered a "reload"? It looks like you are trying to implement some sort of authentication check. What is being protected? – Drew Reese Oct 14 '22 at 20:16
  • Since you are talking about "pages" I'll assume this to mean specific routes you are rendering content for, and that you are trying to protect *some* routes, and that you don't want auth redirects to happen when the user reloads the webpage in the browser and an auth check hasn't completed when the app reloads. Does something like this [answer](/a/66289280/8690857) help? – Drew Reese Oct 14 '22 at 20:27

0 Answers0