Encountering a login issue in my Django application while using Axios and React-Redux. The problem arises when providing incorrect login credentials, resulting in the LOGIN_FAIL action. However, when providing the correct information, the LOGIN_SUCCESS action fails to work and throws the error "Uncaught (in promise) TypeError: Cannot read property 'data' of undefined". This situation has left me puzzled! Interestingly, logging in using Postman works without any issues. How can I resolve this perplexing problem? Any assistance would be greatly appreciated.
Below is my action code:
import axios from "axios";
import {
errorMessage
} from "./messages";
import {
USER_LOADING,
USER_LOADED,
AUTH_ERROR,
LOGIN_SUCCESS,
LOGIN_FAIL
} from "./types";
// Login User
export const login = (username, password) => dispatch => {
// Headers
const config = {
headers: {
"content-type": "application/json"
}
}
// Request body
const body = JSON.stringify({
username,
password
});
axios.post("/api/auth/login", body, config)
.then(res => {
dispatch({
action: LOGIN_SUCCESS,
payload: res.data
});
}).catch(err => {
dispatch(errorMessage(err.response.data, err.response.status));
dispatch({
type: LOGIN_FAIL
});
});
}