Skip to content

Instantly share code, notes, and snippets.

@mksglu
Created May 24, 2018 18:12
Show Gist options
  • Save mksglu/77b3c31083b73c1abea51b7c53ace411 to your computer and use it in GitHub Desktop.
Save mksglu/77b3c31083b73c1abea51b7c53ace411 to your computer and use it in GitHub Desktop.
import React from 'react'
import { connect } from 'react-redux'
import { Redirect, Route } from 'react-router-dom'
const PrivateRoute = ({ component: Component, ...rest }) => {
const { authentication } = rest
return (
<Route
{...rest}
render={props =>
(authentication.loggedIn ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: '/auth/login',
state: { from: props.location },
}}
/>
))
}
/>
)
}
const mapStateToProps = state => ({
authentication: state.authentication,
})
export default connect(mapStateToProps)(PrivateRoute)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment