Created
October 20, 2020 20:00
-
-
Save iosorin/98a13a4d58825ded18810670f0c142bc to your computer and use it in GitHub Desktop.
PrivateRoute, react-ts
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, { ComponentType, FC } from 'react'; | |
| import { useSelector } from 'react-redux'; | |
| import { Redirect, Route, RouteProps } from 'react-router-dom'; | |
| import { getAuthenticated } from '@/store/selectors'; | |
| type PrivateRouteProps = RouteProps & { | |
| component: ComponentType; | |
| }; | |
| const PrivateRoute: FC<PrivateRouteProps> = ({ component: Component, ...rest }) => { | |
| const isAuthenticated = useSelector(getAuthenticated); | |
| return ( | |
| <Route | |
| render={(props) => (isAuthenticated === true ? <Component {...props} /> : <Redirect to="/" />)} | |
| {...rest} | |
| /> | |
| ); | |
| }; | |
| export default PrivateRoute; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment