Created
October 15, 2021 11:48
-
-
Save paoloiulita/f3888ec3ea9349b1b2174f4c9c3d2e17 to your computer and use it in GitHub Desktop.
Protected Routes
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
const routes = getRoutes(user.permissions) | |
<Switch> | |
{routes.map((route, i) => ( | |
<Route | |
key={i} | |
path={route.path} | |
render={props => { | |
const Layout = route.layout | |
const Comp = route.component | |
return ( | |
<Layout> | |
<ErrorBoundary> | |
<Comp /> | |
</ErrorBoundary> | |
</Layout> | |
); | |
}} | |
exact={route.exact} | |
/> | |
))} | |
<Route component={NoMatch} /> | |
</Switch> |
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
const commonRoutes = [ | |
{ | |
id: 'home', | |
path: '/', | |
component: Home, | |
exact: true, | |
}, | |
{ | |
id: 'logout', | |
path: '/logout', | |
component: Logout, | |
}, | |
{ | |
id: 'support', | |
path: '/support', | |
component: Support | |
} | |
] | |
const reservedRoutes = { | |
BACKOFFICE: { | |
id: 'admin', | |
path: '/admin', | |
component: Admin, | |
layout: Default | |
} | |
} | |
export default permissions => { | |
const routes = [...commonRoutes] | |
if (!permissions) return routes | |
forEach(keys(permissions), key => { | |
if (permissions[key] && reservedRoutes[key]) routes.push(reservedRoutes[key]); | |
}) | |
return routes | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment