I cannot redirect from the Login page to the /profile page after clicking the Login button. How can I fix that?
import React from "react";
import { useHistory } from "react-router-dom";
function Login() {
const history = useHistory();
function checkLogin() {
const loginFormUser = document.getElementById("login-form-user");
const loginFormPassword = document.getElementById("login-form-password");
const loginButton = document.getElementById("login-form-submit");
loginButton.addEventListener("click", () => {
const username = loginFormUser.username.value;
const password = loginFormPassword.password.value;
if (username === "toilacoder" && password === "123456") {
return history.push("/profile");
} else {
return history.push("/home");
}
});
}
return (
<form onSubmit={checkLogin}>
<h2>Please login to get access to your profile.</h2>
<input type="text" placeholder="Username" id="login-form-user" />
<input
type="password"
placeholder="Your password"
id="login-form-password"
/>
<button id="login-form-submit" type="submit">
Login
</button>
</form>
);
}
export default Login;