Created
April 15, 2017 15:58
-
-
Save Metnew/fdb3cbd53cac3512651ae0b2ba8279cf to your computer and use it in GitHub Desktop.
route protected by authentication react-router 4
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, {Component} from 'react' | |
import PropTypes from 'prop-types' | |
import {Route, Redirect} from 'react-router' | |
/** | |
* Component that protects route from unauthorized users. | |
* @type {Object} | |
*/ | |
class RouteAuth extends Component { | |
constructor(props) { | |
super(props) | |
} | |
static propTypes = { | |
canAccess: PropTypes.bool, | |
component: PropTypes.func, | |
path: PropTypes.string, | |
name: PropTypes.string, | |
exact: PropTypes.bool, | |
strict: PropTypes.bool | |
} | |
render() { | |
let {canAccess, component, path, name, exact, strict} = this.props | |
let routeProps = { | |
path, | |
component, | |
name, | |
exact, | |
strict | |
} | |
return canAccess ? <Route {...routeProps} /> : <Redirect to="/auth" /> | |
} | |
} | |
export default RouteAuth |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment