I was trying to create a basic login form, but after successful login, I could not get my page to redirect to another URL. Instead, it was loading the redirected page on the same URL.
app.post("/login", async (req, res) => {
try{
const email = req.body.email;
const password = req.body.password;
const userEmail = await Users.findOne({email:email});
if(userEmail.password === password) {
// res.render("index.hbs");
res.send("Wait for it");
setTimeout(function() {
window.open("localhost:port/home");
console.log("This is new url");
}, 500);
} else {
res.send("Invalid login details");
}
} catch {
res.send("Invalid login details").status(400);
}
})
Use the useNavigate to fixed your problem import { useNavigate } from "react-router-dom"
const AnalyticTracker = (props) => {
const navigate = useNavigate();
return (
<form onSubmit={() => navigate('../', { replace: true })}>
{...}
</form>
)
)