How Do i implement Login using Axios so i can get the username and password i have done this with Fetch, but for some reason, even after setting CORS, Normally I use Fetch, But i have not tried out with Axios. This is my code for Fetch which works fine, which is supposed to work, but somehow returns problems in the long run..
login = () =>{
const { email, password } = this.state;
const body = { email, password };
fetch('http://localhost:5001/auth/sign_in',{
method: 'POST',
mode: 'cors',
headers:{
'Accept' : 'application/json',
'Content-type':'application/json'
},
body:body
}).then((response)=>response.json())
.then((responseJson)=>{
if(responseJson.message ==='OK'){
localStorage.setItem('token',responseJson.token);
<Navigate to="/dashboard" />
}
})
}
How Do I convert this into Axios?
CodePudding user response:
Fetch worked afterall. I had to install cors on the REST api (Backend) and after that everything appears to be fine
CodePudding user response:
You only have to check the documentation... But here is an example :
await axios.post(
'http://localhost:3001/login',
{
user_name: this.state.user_name,
password: this.state.password,
},
{headers}
).then(response => {
console.log("Success ========>", response);
})
.catch(error => {
console.log("Error ========>", error);
}
)