Hi I'm working with a Laravel API for the backend and making a Login with Redux in React. Well I call the API and I want the laravel API to pass a precise error of what is happening for example the user doesn't exist. The problem is when I want to acces the mesage in the catch part of the call made with axios
Laravel Login part
public function login(Request $request){
$loginData = $request->validate([
'name' => 'required',
'password' => 'required'
]);
if (!auth()->attempt($loginData)) {
return response(["message" => "I want to acces this message in the catch part of Axios"], 401);
}
$accessToken = auth()->user()->createToken('authToken')->accessToken;
return response(['user' => auth()->user(), 'access_token' => $accessToken]);
}
React call with axios
Axios({
method: 'post',
url: '/login',
data: {
name: name,
password: password,
}
}).then(function (response) {
dispatch(authSuccess(response.data.access_token, response.data.user.id));
})
.catch(function (response) {
console.log(response)
});
Response I get in the web console
xhr.js:177 POST http://127.0.0.1:8000/api/login 401 (Unauthorized)
and I want to get the message i pass in the Laravel API ("I want to acces this message in the catch part of Axios")