I am making a rest api, and am aware that an incorrect username or password should return a status code of 401. The problem is I can't send an error message in the response with this status code.
Is there anything wrong with sending a status code of 200 when a user isn't found so that I can send a message to the front end:
User.findOne({ email: req.body.email }).then((user) => {
if (!user) {
res.status(200).json({ message: 'Username not found'});
return;
}
If I send a 400 or 401, then if I make an axios request to the route it rejects the promise and I can't receive the message. Is there any way to return a status code of 400/401 and send json back? I would greatly appreciate any help. This is my front end call to the route:
export const loginUser = ({ email, password}) =>
async (dispatch, getState) => {
const res = await axios.post(`/api/login`, {email, password}).catch(() => {
dispatch({ type: 'AUTH_ERROR', payload: res.data.message }) //error here because no res
});
dispatch({
type: 'USER_LOGIN',
payload: res.data._id
});
}