5

I'm using react router v4 with redux and i want to make use of private and protected route to redirect if user is not logged in.

i have this Routes component:

class Routes extends Component {

render() {
    const { auth } = this.props;
    // so checks against isAuthenticated can pass
    if (auth.isAuthenticated !== undefined) {
        return (
            <div>
                <Route exact component={() => <Home />} />
                <PublicRoute
                    authed={() => auth.isAuthenticated}
                    path="/login"
                    component={(routerProps) => <Login {...routerProps} />}
                />
                <PublicRoute
                    authed={() => auth.isAuthenticated}
                    path="/register"
                    component={(routerProps) => <Register {...routerProps} />}
                />
                <PrivateRoute
                    authed={() => auth.isAuthenticated}
                    path="/dashboard"
                    component={Dashboard}
                />
            </div>
        );
    } else {
        return <div></div>
    }
  }
}

function mapStateToProps(state) {
  return {
    auth: state.auth
  }
}

export default withRouter(connect(mapStateToProps)(Routes));

it is implemented like this:

class Main extends Component {

constructor(props) {
    super(props);
}

componentDidMount() {
    store.dispatch(checkAuth());
}

render() {
    return (
        <Provider store={store}>
            <Router>
                <Theme>
                    <Routes />
                </Theme>
            </Router>
        </Provider>
    );
  }
}

This is the PrivateRoute:

export function PrivateRoute({ component: Component, authed, ...rest }) {
const isAuthenticated = authed();
return (
    <Route
        {...rest}
        render={props =>
            isAuthenticated === true ? (
                <Component {...props} />
            ) : (
                <Redirect
                    to={{
                        pathname: "/login",
                        state: { from: props.location }
                    }}
                />
            )
        }
    />
  );
}

What is the best way to pass that auth prop, is it ok if i connect the Routes component and pass the auth from there, or should i be passed from somewhere else, maybe connect each component that needs it and read from there? Also how would this approach play with nested routes, like /dashboard/settings? Thanks

Case09
  • 313
  • 6
  • 18

1 Answers1

2

Actually, it is ok to use this type of private route in react, but you should check two moments:

  1. I should check, that you do not have exact attribute, so all your routes like /dashboard/panel1, /dashboard/panel2 will be private to

    auth.isAuthenticated} path="/dashboard" component={Dashboard} />

  2. You will have some problem with connect. There is a simple fix for that:

    export default connect(mapStateToProps, null, null, { pure: false, })(PrivateRoute);

more information here: React router private routes / redirect not working

MBehtemam
  • 7,865
  • 15
  • 66
  • 108
Slawa Eremin
  • 5,264
  • 18
  • 28