How can I redirect to Home page ('/') once a user is logged in?
async function onHandleSubmit(e: SyntheticEvent) {
e.preventDefault()
if (login && password) {
await fetch('URL/auth/signin', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
"login": login,
"password": password
})
}).then(response =>
response.json().then(data => ({
data: data,
})).then(res => {
localStorage.setItem("token", res.data.token);
localStorage.setItem("userName", res.data.name)
localStorage.setItem("avatarUrl", res.data.avatarUrl)
})
).then(() => {
setLogin('')
setPassword('')
})
}
}
CodePudding user response:
Assuming you are not using redux. You may redirect using React Router Dom as following
import { useHistory } from "react-router-dom";
const history = useHistory();
history.push("/dashboard"); // Add this in Then part where Localstorage set.