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. 7
      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';
class NavBarContainer extends Component {
constructor(props){
super(props);
}
render() {
return (
<Menu fixed='top' inverted>

7
app/src/routes.js

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

Loading…
Cancel
Save