After sending an axios post request from react to the server when trying to add an if case for the res.status === 401 the app ignores it, it is though handling the cases for status code 200. Trying to deal with this for a few days now and haven't come to a solution.
const login = () => {
axios({
method: "POST",
data: {
email: loginEmail,
password: loginPassword
},
withCredentials: true,
url: "http://localhost:3000/login"
}).then((res) => {
if (res.status === 401) {
console.log(res.status);
toast.error("Something went wrong!");
} else {
handleData(res.data);
handleLoggedOut(false);
toast.success("You are now logged in!");
}
});
};

Tried different options like: if (!res) or if (res.error), even if(res.status = "401 (Unauthorized)") although the code is clearly 401 but nothing seems to work, seems like the only response recognized is the one with status 200, for a successfull login the function does its job but in case of an unsuccessfull one nothing happens, just the error with status 401 in the console.
If anyone has any ideas why this is happening please let me know.
Thanks in advance!