2

Setting up authentication with reactjs, firebase (google auth), react-router and redux.

The problem is very simple but I can't find any resource online or answers to fix it.

Unable to read roperty of uid (user id with firebase) because it's telling me it's undefined? I've set this up that Private routes is a new component and it's been imported in my app router. I also plan to have a public routes as well.

Here is my code along with screenshots of error.

PrivateRoute.js

import React from 'react';
import { connect } from 'react-redux';
import { Route, Redirect } from 'react-router-dom';

export const PrivateRoute = (props) => (
    <Route {...props} />
  );

const mapStateToProps = (state) => ({
  isAuthenticated: !!state.auth.uid <-error on this uid
});

export default connect(mapStateToProps)(PrivateRoute);

AppRouter.js

import PrivateRoute from './PrivateRoute'

<Route path="/" component={Login} exact={true}/>
<PrivateRoute path="/dashboard" component={Dashboard} />
<PrivateRoute path="/create" component={AddExp}/>

Screenshot of error when I'm logged out and try accessing /create private route

Console errors: chrome

Updated to add redux store configure file

import authenticationReducer from '../reducers/authentication'

export default () => {
    const store = createStore(
        combineReducers({
            expenses: expensesReducer,
            authentication: authenticationReducer
        }),
        composeEnhancers(applyMiddleware(thunk))
    );
    return store;
};

Auth reducer (just incase it's needed)

export default (state = {}, action) => {
    switch (action.type) {
        case 'LOGIN':
        return {
            uid: action.uid
        };
        case 'LOGOUT':
        return {

        };
        default:
        return state;
    }
};
4
  • 1
    Going to need to see how you are setting auth in your store Commented Mar 16, 2018 at 21:49
  • One moment sorry Commented Mar 16, 2018 at 21:49
  • Thank you this worked! I could not see this... Commented Mar 16, 2018 at 21:54
  • I thought it might be a problem with firebase uid Commented Mar 16, 2018 at 21:55

2 Answers 2

3

Cannot read property 'uid' of undefined - means you are trying something like variable.uid and variable is undefined. Based on the line with the error, state.auth is undefined.

You should be able to look at your state there, either debug or just throw a console.log in your mapStateToProps to see what your state actually looks like:

const mapStateToProps = (state) => {
  console.log('state:', state); // see what state is
  return {
    isAuthenticated: !!state.auth.uid <-error on this uid
  };
}

Looking at combineReducers is seems like you are putting the result of your authenticationReducer onto state.authentication, not state.auth...

combineReducers({
    expenses: expensesReducer,
    authentication: authenticationReducer
}),
Sign up to request clarification or add additional context in comments.

Comments

1

You are setting the uid on state.authentication.uid and trying to access it from state.auth.uid

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.