Currently, my code is communicating with my django-rest-auth api. It is getting the token, however, it is not authenicating the request. My backend shows 200 (username and password is correct). While my front end, reactjs is declaring wrong username or password (i created in else statement) and a 400 bad request error.
axios
.post(
'http://127.0.0.1:8000/rest-auth/login/',
{
username: this.state.username,
password: this.state.password
},
// { withCredentials: true }
{ isAuthenticated: true }
)
.then(response => {
const token = response.data.key;
if (localStorage.getItem(token)) {
console.log(token);
this.props.handleSucessfulAuth();
} else {
this.setState({
errorMessage: 'Wrong username or password'
});
this.props.handleUnsuccessfulAuth();
// console.log(token);
}
})
.catch(error => {
this.setState({
errorMessage: 'Authorization error occured',
error
})
this.props.handleUnsuccessfulAuth();
});
}
Now I have changed the top portion to the following:
handleSubmit(event) {
event.preventDefault();
axios.defaults.headers.common["Authorization"] = `Token ${this.props.token}`;
axios
.post(
'http://127.0.0.1:8000/rest-auth/login/',
{
username: this.state.username,
password: this.state.password
},
// { withCredentials: true }
{ isAuthenticated: true }
)
.then(response => {
const token = response.data.key;
if (localStorage.setItem(token, 'token')) {
console.log(token);
this.props.handleSucessfulAuth();
} else {
this.setState({
errorMessage: 'Wrong username or password'
});
this.props.handleUnsuccessfulAuth();
// console.log(token);
}
})