0
  const loginUser = async (e) => {
    e.preventDefault();

    await axios.post(
      "http://localhost:5000/api/login",
      {
        email,
        password,
      },
      { withCredentials: true }
    );
    fetchAllCars();

  //I want to push the user to their history section here
  
    console.log("success");
  };

I wrote a logic that redirect a user to login page whenever they click on a protected route. This check if the user had signed in, if not, the logic will redirect the user to login page. So when the credentials are correct, I would like to return the user to the previous state in the home page before redirecting them to login page

juliomalves
  • 21,997
  • 12
  • 75
  • 81
Paulliano
  • 47
  • 1
  • 7
  • Are you trying to redirect back from within the client code, or on the server side with Nextjs? Your question is a bit unclear. – Drew Reese Feb 17 '22 at 08:17
  • 1
    Can you update your question to include all relevant code you are trying to work with in a [minimal, complete, and reproducible code example](https://stackoverflow.com/help/minimal-reproducible-example)? – Drew Reese Feb 17 '22 at 08:34
  • I've edited the question. – Paulliano Feb 17 '22 at 10:54
  • I see, so you are asking how to make the UI code redirect back after authentication. Does this answer your question? https://stackoverflow.com/a/66289280/8690857 If not, then can you update your question to include all relevant UI code handling this authentication check and redirection to login route/component? – Drew Reese Feb 17 '22 at 17:56
  • Does this help answer your question: [How to redirect back to private route after login in Next.js?](https://stackoverflow.com/a/70689821/1870780)? You'll need to implement your own logic to redirect back to the previous page. – juliomalves Feb 18 '22 at 23:54
  • I want the user to return to what they're doing before redirecting them – Paulliano Feb 19 '22 at 05:07

0 Answers0