how to validate login email & password using redux? a already make a handleLogin function it work fine when user input the right email&password but if user submit the wrong email&password the api crash
const handleClick = (e) => {
e.preventDefault()
try {
const masuk = login(dispacth, { email, password })
// history.push("/")
} catch (error) {
Swal.fire({
position: 'top-end',
icon: 'error',
title: 'Email atau Password tidak sesuai',
showConfirmButton: false,
timer: 1500,
})
}
}
and my dispatch
export const login = async (dispatch, user) => {
dispatch(loginStart())
try {
const res = await axios.post(BASE_URL + 'auth/login', user)
dispatch(loginSuccess(res.data))
} catch (error) {
dispatch(loginFailure(error))
}
}
2
Answers
You are calling an async function without using
await
so the promise is not handled properly.Try:
you need to run validation before the submission of the form, here is how you would do it:
but I recommend you to check out React Hook Form which handles everything you think of related to forms and validations
Example with react hook form:
make sure you have react-hook-form and yup installed in your project: