Skip to content

Instantly share code, notes, and snippets.

@michaelhelvey
Last active February 11, 2019 00:49
Show Gist options
  • Save michaelhelvey/b8a1c62fcb4ae3fc30945785ff9ec90a to your computer and use it in GitHub Desktop.
Save michaelhelvey/b8a1c62fcb4ae3fc30945785ff9ec90a to your computer and use it in GitHub Desktop.
React-Router authorized route
const RequireAuth = connect(mapStateToProps)(
({ component: Component, ...rest }) => {
return (
<Route
{...rest}
render={props =>
rest.isAuthenticated ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: '/login',
state: { from: props.location }
}}
/>
)
}
/>
)
}
)
import * as React from 'react'
import { Route, Redirect } from 'react-router-dom'
export const PrivateRouteComponent = (rest: any) => {
return (
<Route
{...rest}
render={(props: any) =>
rest.isLoggedIn ? (
<rest.AuthComponent {...props} />
) : (
<Redirect
to={{
pathname: '/',
state: { from: props.location },
}}
/>
)
}
/>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment