Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login error cannot read property 'data' #13

Open
rade98 opened this issue Jun 18, 2019 · 7 comments
Open

Login error cannot read property 'data' #13

rade98 opened this issue Jun 18, 2019 · 7 comments

Comments

@rade98
Copy link

rade98 commented Jun 18, 2019

Hi,
I'm having some problems with frontend authentication.
I'm following all the steps from videos, and until now everything worked well. But now I have issues with login.
When I give invalid username/password I can see in redux extension 'LOGIN_FAIL' but when I log in with an actual account I don't see 'LOGIN_SUCCESS'. In console I see

Uncaught (in promise) TypeError: Cannot read property 'data' of undefined at eval (auth.js:54)

I tried to console.log(res) even console.log(res.data) everything looks fine. Why is that?
Here is my auth.js

import axios from 'axios';
import { returnErrors } from './messages';
import { USER_LOADED, USER_LOADING, AUTH_ERROR, LOGIN_SUCCESS, LOGIN_FAIL } from './types';

// Check token and load user
export const loadUser = () => (dispatch, getState) => {
    // User Loading
    dispatch({ type: USER_LOADING });

    // Get Token from state
    const token = getState().auth.token;

    // Headers
    const config = {
        headers: {
            "Content-Type": "application/json"
        }
    };

    // If token, add to headers
    if (token) {
        config.headers['Authorization'] = `Token ${token}`;
    }

    axios.get('/api/auth/user', config)
        .then(res => {
            dispatch({
                type: USER_LOADED,
                payload: res.data
            });
        }).catch(err => {
            dispatch(returnErrors(err.response.data, err.response.status));
            dispatch({
                type: AUTH_ERROR
            });
        });
};


// Login iser
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({
                type: LOGIN_SUCCESS,
                payload: res.data
            });
        })
        .catch(err => {
            dispatch(returnErrors(err.response.data, err.response.status));
            dispatch({
                type: LOGIN_FAIL
            });
        });
};

I even have downloaded this code, I was checking all of this, even replacing with your code but I got the same error, maybe it's not a problem with auth.js?

@rade98
Copy link
Author

rade98 commented Jun 18, 2019

Anyone?

@linneudm
Copy link

linneudm commented Jul 3, 2019

same problem here =(

@linneudm
Copy link

linneudm commented Jul 3, 2019

I do not know why but when I copied the static files from the repository it worked, except this line in Alert.js should look like this:

export default connect(mapStateToProps)(withAlert()(Alerts));

@deerajDev
Copy link

@linneudm . Try to use the alerts version used in the tutorial.The latest version is not compatible with the code . or you can also try react-toastify

@deerajDev
Copy link

Try console.log(Object.Keys(res)) . It will give all the keys of an object. If you don't find data key, then can you send the link of the repository

@kaloster
Copy link

@rade98 @linneudm any success with figuring this out? I'm having the same issue.

@cpinamtz
Copy link

cpinamtz commented Jul 1, 2020

@kaloster @linneudm @rade98 After a couple of weeks blocked in the same step, I found the solution. Basically the problem is related to a wrong spelling of the variables. Please do the following:

Check carefully how you've spelt the auth.isAuthenticated variable inside your PrivateRoute.js. In my case, It was set as auth.IsAuthenticated so when the custom router decides if render your current component or redirect you to the /login page it will do the second due to there isn't any auth.IsAuthenticated attribute so !auth.IsAuthenticated is true and it will constantly go back and forth because probably in your Login.js conditional statement is correctly declared.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants