Skip to content

Instantly share code, notes, and snippets.

@Metnew
Created April 23, 2017 17:10
Show Gist options
  • Save Metnew/a447a53d4ed4e04d47ea5e1563828e8f to your computer and use it in GitHub Desktop.
Save Metnew/a447a53d4ed4e04d47ea5e1563828e8f to your computer and use it in GitHub Desktop.
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 {
static propTypes = {
canAccess: PropTypes.func,
component: PropTypes.func,
path: PropTypes.string,
exact: PropTypes.bool,
strict: PropTypes.bool
}
render () {
let {canAccess, component, path, exact, strict} = this.props
let routeProps = {
path,
component,
exact,
strict
}
return canAccess(path) ? <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