Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save deyvisonborges/e175f9fc9482f61786197e3024997026 to your computer and use it in GitHub Desktop.
Save deyvisonborges/e175f9fc9482f61786197e3024997026 to your computer and use it in GitHub Desktop.
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