Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Metnew/fdb3cbd53cac3512651ae0b2ba8279cf to your computer and use it in GitHub Desktop.
Save Metnew/fdb3cbd53cac3512651ae0b2ba8279cf to your computer and use it in GitHub Desktop.
route protected by authentication react-router 4
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