1

My Action.js login function is as follows:

export const auth = (username, password) => {
return dispatch => {
    dispatch(authStart());
    const config = {
        headers: {
          'Content-Type': 'application/json'
        }
      };

    const authData = {
        username: username,
        password: password,
        returnSecureToken: true
    }

    axios.post("http://localhost:4000/scripts/auth/login/", authData, config)
        .then(response => {
            localStorage.setItem('token', response.data.token);
            localStorage.setItem('userId', response.data.user.id);
            dispatch(authLoginSuccess(response.data.token, response.data.user.id));
        })
        .catch(err => {
            dispatch(authFail(err))
        })
}

}

Django Rest return following detail upon bad credentials:

{
"non_field_errors": [
    "Unable to log in with provided credentials."
]

}

But when I am logging the error to console, I don't see any fields like non_field_errors. The err object has the following data as shown by redux dev tools:

Error: Request failed with status code 400
at createError (createError.js:17)
at settle (settle.js:19)
at XMLHttpRequest.handleLoad (xhr.js:60)

How to get this message from API response?

1 Answer 1

1

Nevermind. I got it !!!

err.response has the actual details

console.log(err.response)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.