Browse Source

Add private routes

develop
Apostolos Fanakis 6 years ago
parent
commit
80cf844c6a
  1. 28
      app/src/PrivateRoute.js
  2. 4
      app/src/containers/NavBarContainer.js
  3. 9
      app/src/routes.js

28
app/src/PrivateRoute.js

@ -0,0 +1,28 @@
import React from 'react'
import {connect} from 'react-redux';
import { Route, Redirect } from 'react-router-dom'
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={props =>
props.hasSignedUp ? (
<Component {...props} />
) : (
<Redirect to={{
pathname: "/signup",
state: { from: props.location }
}}
/>
)
}
/>
);
const mapStateToProps = state => {
return {
hasSignedUp: state.user.hasSignedUp,
}
};
export default connect(mapStateToProps)(PrivateRoute);

4
app/src/containers/NavBarContainer.js

@ -8,10 +8,6 @@ import { Image, Menu } from 'semantic-ui-react'
import logo from '../assets/images/logo.png'; import logo from '../assets/images/logo.png';
class NavBarContainer extends Component { class NavBarContainer extends Component {
constructor(props){
super(props);
}
render() { render() {
return ( return (
<Menu fixed='top' inverted> <Menu fixed='top' inverted>

9
app/src/routes.js

@ -1,6 +1,7 @@
import React from 'react' import React from 'react'
import { Route, Switch } from 'react-router-dom' import { Route, Switch, Redirect } from 'react-router-dom'
import NavBarContainer from "./containers/NavBarContainer"; import PrivateRoute from './PrivateRoute.js';
import NavBarContainer from './containers/NavBarContainer';
import HomeContainer from './containers/HomeContainer' import HomeContainer from './containers/HomeContainer'
import SignUpContainer from './containers/SignUpContainer' import SignUpContainer from './containers/SignUpContainer'
import NotFound from './components/NotFound' import NotFound from './components/NotFound'
@ -10,11 +11,11 @@ const routes = (
<div> <div>
<NavBarContainer /> <NavBarContainer />
<Switch> <Switch>
<Route exact path="/" component={HomeContainer} /> <PrivateRoute exact path="/" component={HomeContainer} />
<Route path="/signup" component={SignUpContainer} /> <Route path="/signup" component={SignUpContainer} />
<Route component={NotFound} /> <Route component={NotFound} />
</Switch> </Switch>
</div> </div>
); );
export default routes export default routes

Loading…
Cancel
Save