Redirect on Logout

Hey! First of all, thanks for the awesome tutorial! I’ve been having a lot of fun going through it.

However, on the “Redirect on Login and Logout” chapter I get this error when trying to redirect after logout -

"Uncaught (in promise) TypeError: Cannot read property 'push' of undefined"

The Login redirect works, which is super weird, because I’m doing the same thing in both places.

This is my handleLogout function:

  async function handleLogout() {
    await Auth.signOut();
  
    userHasAuthenticated(false);
  
    history.push("/login");
  }

and the handleSubmit function in Login.js:

  async function handleSubmit(event) {
    event.preventDefault();

    setIsLoading(true);

    try {
      await Auth.signIn(fields.email, fields.password);
      userHasAuthenticated(true);
      history.push("/main");
    } catch (e) {
      onError(e);
      setIsLoading(false);
    }
  }

I’m importing useHistory in both files and calling the variable with const history = useHistory();

I’ve been staring at this for so long, I don’t know what might be the cause of this.

I’d appreciate any help! Thanks!

1 Like

That’s really odd. The error makes it sound like something is wrong with React Router. Can you compare your code to the one in the repo: