Created
August 6, 2024 14:24
-
-
Save deyvisonborges/e175f9fc9482f61786197e3024997026 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 { Route, Redirect } from 'react-router-dom'; | |
function App() { | |
const isAuthenticated = true; // Replace with actual authentication check | |
return ( | |
<Router> | |
<Switch> | |
<Route exact path="/" component={HomePage} /> | |
<Route exact path="/about" component={AboutPage} /> | |
<Route exact path="/contact" component={ContactPage} /> | |
<PrivateRoute exact path="/dashboard" component={DashboardPage} isAuthenticated={isAuthenticated} /> | |
</Switch> | |
</Router> | |
); | |
} | |
function PrivateRoute({ component: Component, isAuthenticated, ...rest }) { | |
return ( | |
<Route {...rest} render={(props) => ( | |
isAuthenticated ? <Component {...props} /> | |
: <Redirect to={{ pathname: '/login', state: { from: props.location } }} /> | |
)} /> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment