Skip to content

Instantly share code, notes, and snippets.

@paoloiulita
Created October 15, 2021 11:48
Show Gist options
  • Save paoloiulita/f3888ec3ea9349b1b2174f4c9c3d2e17 to your computer and use it in GitHub Desktop.
Save paoloiulita/f3888ec3ea9349b1b2174f4c9c3d2e17 to your computer and use it in GitHub Desktop.
Protected Routes
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>
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