Last active
October 23, 2019 14:05
-
-
Save aderbas/7fe3f89ee041c826119d6390ee466fe3 to your computer and use it in GitHub Desktop.
React private router
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
/** | |
* Private router | |
* @author: Aderbal Nunes <[email protected]> | |
*/ | |
import React from "react"; | |
import {Route, Redirect} from 'react-router-dom'; | |
// my session manager component | |
import session from './session'; | |
function PrivateRouter({component: Component, ...rest}){ | |
const {sharedObject} = rest; | |
return ( | |
<Route | |
{...rest} | |
render={props => session.isAutenticated() | |
? <Component sharedObject={sharedObject} {...props} /> | |
: <Redirect | |
to={{ | |
pathname: session.isAutenticated()?'/home':'/login', | |
state: {from: props.location} | |
}} | |
/> | |
} | |
/> | |
); | |
} | |
export default PrivateRouter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment