-2
I’m making a login screen, I have a function that posts an API sending user and password, and gets a token back from being correct (so far so good), or returns error 401, which is shown in the console for me.
I wanted to pick up this error and play on an Alert on the screen when the user or password is wrong.
I tried so:
try{
apiPost({
username:this.state.email,
password: this.state.password
}).then((chave) => {
this.setState({token: chave});
this.setState({redirect: '/logado'});
});
}catch(erro){
//console.log(erro);
alert('Usuario ou senha inválidos!');
}
But it seems that it does not fall in the catch, because this Alert that I placed is not launched on the screen, maybe because it is right, but returns with code 401?
My API function is:
export default function apiPost(data){
return(
axios.post('https://minhaapi/api/login',data)
.then((response) => {
console.log('RESPOSTA',response.data);
if(response.status === "sucess"){
return response.data.token;
}
return response.data.status
})
);
}
I tried to use the status, unsuccessfully, when the data is right, it returns the token right, and I redirect, when the error, it shows the message on the console, and only! want to inform a message, where should I do it ? and how ?
Could show your api code?
– Danizavtz