Created
October 29, 2017 10:05
-
-
Save Aperyon/93167387759be1efa629090286dfbf9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ import LoginRequiredRoute from './LoginRequiredRoute'; | |
<Switch> | |
<Route path="/login/" component={Login} /> | |
+ <LoginRequiredRoute component={App} /> | |
- <Route component={App} /> | |
</Switch> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// changes only | |
import { changeLoggedIn } from './actions'; | |
const App = (props) => { | |
return ( | |
<div> | |
<Nav /> | |
<button onClick={() => { props.changeLoggedIn(false) }}>Logout</button> | |
<Switch> | |
<Route exact path="/friends/" component={Friends} /> | |
<Route exact path="/books/" component={Books} /> | |
<Redirect exact from="/" to="/friends/" /> | |
</Switch> | |
</div> | |
) | |
} | |
export default connect( | |
state => ({ | |
loggedIn: state.loggedIn, | |
}), | |
{ | |
changeLoggedIn | |
} | |
)(App); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { connect } from 'react-redux'; | |
import { Redirect } from 'react-router-dom'; | |
import { changeLoggedIn } from './actions'; | |
const Login = (props) => { | |
if (props.loggedIn) { | |
const { from } = props.location.state || { from: { pathname: '/' } } | |
return <Redirect to={from} /> | |
} | |
return ( | |
<div> | |
<h1>Login</h1> | |
<button onClick={() => { props.changeLoggedIn(true) }}>Login</button> | |
</div> | |
) | |
} | |
export default connect( | |
state => ({ | |
loggedIn: state.loggedIn | |
}), | |
{ | |
changeLoggedIn, | |
} | |
)(Login); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { connect } from 'react-redux'; | |
import { Redirect, Route } from 'react-router-dom'; | |
const LoginRequiredRoute = ({ component: Component, ...rest }) => ( | |
<Route {...rest} render={props => ( | |
rest.loggedIn ? ( | |
<Component {...props} /> | |
) : ( | |
<Redirect to={{ | |
pathname: '/login/', | |
state: { from: props.location } | |
}} /> | |
) | |
)} /> | |
) | |
export default connect( | |
state => ({ | |
loggedIn: state.loggedIn, | |
}) | |
)(LoginRequiredRoute); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment