Skip to content

Instantly share code, notes, and snippets.

@buglessir
Last active October 29, 2019 13:20
Show Gist options
  • Save buglessir/f057a53ea4760500264c979426163a63 to your computer and use it in GitHub Desktop.
Save buglessir/f057a53ea4760500264c979426163a63 to your computer and use it in GitHub Desktop.
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter, Switch, Route, Link, Redirect } from 'react-router-dom';
function Auth() {
return false;
}
const Admin = () => (
<h1>This is Admin page</h1>
)
const Login = () => (
<h1>Login Page</h1>
)
const Home = () => (
<h1>Home Page</h1>
)
const ProtectedRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={(props) => (
Auth() === true
? <Component {...props} />
: <Redirect to='/login' />
)} />
)
const App = () => (
<>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/admin">Admin</Link>
</li>
</ul>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/login" component={Login} />
<ProtectedRoute path="/admin" component={Admin} />
</Switch>
</>
)
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById('root')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment