Created
August 24, 2020 03:02
-
-
Save marcovega/1d64b5dedcd3718b44dfcb5067a01588 to your computer and use it in GitHub Desktop.
This file contains 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 * as React from 'react'; | |
import { Redirect, Route, RouteProps } from 'react-router'; | |
export const PrivateRoute: React.FC<RouteProps> = (props) => { | |
if (!localStorage.getItem('user')) { | |
const renderComponent = () => <Redirect to={{ pathname: '/login', state: { from: props.location } }} />; | |
return <Route {...props} component={renderComponent} render={undefined} />; | |
} else { | |
return <Route {...props} />; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment