I'm using passportjs for user authentication, and in the event that authentication is unsuccessful, I'd like to display a custom error message that is rendered on client side (in this case, React/Redux).
Route:
app.post(
'/api/login',
(req, res) => {
passport.authenticate('local-login', (err, user, info) => {
if (!user) return res.status(400).send({ msg: 'Please verify account first' });
})(req, res);
}
);
Action creator:
export const loginUser = (values, history) => async dispatch => {
try {
const res = await axios.post('/api/login', values);
dispatch(deleteMessage());
dispatch({ type: FETCH_USER, payload: res.data });
history.push('/');
window.Materialize.toast(`Welcome back ${res.data.local.email}!`, 4000);
}
catch(err) {
console.log('Error:', err);
}
}
When I catch the error and console log it out however, I get a standard status 400 error message instead of: { msg: 'Please verify account first' }
Error: Request failed with status code 400
at createError (createError.js:16)
at settle (settle.js:18)
at XMLHttpRequest.handleLoad (xhr.js:77)
Is there a better way to display custom error messages?