Created
May 20, 2019 04:13
-
-
Save hckhanh/5570f7c7383f0f513b2df4e1c036d3da 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 React from 'react'; | |
import { Redirect, Route } from 'react-router-dom'; | |
import { checkAuthentication } from '../../store/selectors/user'; | |
function withAuthRoute(checkAuth, pathname) { | |
return function AuthRoute({ component: Component, ...rest }) { | |
return ( | |
<Route | |
{...rest} | |
render={props => { | |
if (checkAuth()) { | |
return <Component {...props} />; | |
} | |
return ( | |
<Redirect to={{ pathname, state: { from: props.location } }} /> | |
); | |
}} | |
/> | |
); | |
}; | |
} | |
export const PrivateRoute = withAuthRoute( | |
() => checkAuthentication(), | |
'/login' | |
); | |
export const PublicOnlyRoute = withAuthRoute( | |
() => !checkAuthentication(), | |
'/workplace/select' | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment