Skip to main content

18.19 Guarding Routes

App.js

src\App.js
import React, { Component } from "react";
import { connect } from "react-redux";
import { Redirect, Route, Switch, withRouter } from "react-router-dom";
import Layout from "./components/Layout/Layout";
import Auth from "./containers/Auth/Auth";
import Logout from "./containers/Auth/Logout/Logout";
import BurgerBuilder from "./containers/BurgerBuilder/BurgerBuilder";
import Checkout from "./containers/Checkout/Checkout";
import Orders from "./containers/Orders/Orders";
import * as actions from "./store/actions/index";

class App extends Component {
componentDidMount() {
this.props.onTryAutoSignup();
}
render() {
let routes = (
<Switch>
<Route path="/auth" component={Auth} />
<Route path="/" exact component={BurgerBuilder} />
<Redirect to="/" />
</Switch>
);

if (this.props.isAuthenticated) {
routes = (
<Switch>
<Route path="/checkout" component={Checkout} />
<Route path="/orders" component={Orders} />
<Route path="/logout" component={Logout} />
<Route path="/" exact component={BurgerBuilder} />
<Redirect to="/" />
</Switch>
);
}

return (
<div>
<Layout>{routes}</Layout>
</div>
);
}
}

const mapStatetoProps = (state) => {
return {
isAuthenticated: state.auth.token !== null,
};
};

const mapDispatchtoProps = (dispatch) => {
return {
onTryAutoSignup: () => dispatch(actions.authCheckState()),
};
};

export default withRouter(connect(mapStatetoProps, mapDispatchtoProps)(App));