so im trying to navigate after the user is logged in. I have tried redirect. history.push and Navigate. iv also tried to return after the localstorage call and still nothing. currently the line used to navigate to a new page is
const handleSubmit = async (e) =>{
e.preventDefault();
try{
const {data} = await axios.post('/api/signin', {
email,
password
});
console.log(data);
if (data.success === true){
setValues({ email: '', password:''});
toast.success("Log In successfully");
localStorage.setItem("token", JSON.stringify(data))
if (typeof window !== "undefined"){
setTimeout(()=>{
Navigate('/user/Userdashboard');
}, 2000);
}
}
also if you know how to refresh page on login that would be awesome
CodePudding user response:
try{
const res = await fetch('/api/signin', {
method:'POST',
body:JSON.stringify({ email,password}),
headers:{'Content-Type':'application/json'}
});
const data = await res.json()
console.log(data);
if(data){
location.assign('/user/Userdashboard');
}
}
catch(err){
console.log(err)
}
CodePudding user response:
const handleSubmit = async (e) =>{
e.preventDefault();
try{
const res = await fetch('/api/signin', {
method:'POST',
body:JSON.stringify({ email,password}),
headers:{'Content-Type':'application/json'}
});
const data = await res.json()
console.log(data);
if(data){
setValues({ email: '', password:''});
toast.success("Log In successfully");
localStorage.setItem("token", JSON.stringify(data))
window.location.assign('/UserDashboard');
}
}
catch(err){
console.log(err)
}
}