Last active
May 30, 2021 05:16
-
-
Save jmaicaaan/1f847d0371c604be4159c0c7f09b6420 to your computer and use it in GitHub Desktop.
Protected Route
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, { useEffect } from 'react'; | |
import { | |
BrowserRouter as Router, | |
Redirect, | |
Route, | |
Switch, | |
} from 'react-router-dom'; | |
import './App.css'; | |
import { AdminDashboard } from './containers/AdminDashboard'; | |
import { Dashboard } from './containers/Dashboard'; | |
import { ProtectedRoute } from './components/ProtectedRoute'; | |
import { AdminGuard } from './guards/admin-guard'; | |
const App = () => { | |
useEffect(() => { | |
// Mocking the local storage to have a user data | |
const user = { | |
firstName: 'JM', | |
lastName: 'Santos', | |
roles: ['admin'], | |
}; | |
window.localStorage.setItem('user', JSON.stringify(user)); | |
}, []); | |
return ( | |
<Router> | |
<div className="App"> | |
<Switch> | |
<ProtectedRoute | |
path="/admin" | |
guards={[AdminGuard]} | |
fallback={() => <Redirect to="/home" />} | |
> | |
<AdminDashboard /> | |
</ProtectedRoute> | |
<Route path="/home"> | |
<Dashboard /> | |
</Route> | |
</Switch> | |
</div> | |
</Router> | |
); | |
}; | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment