I did see Tyler McGinnis https://tylermcginnis.com/react-router-protected-routes-authentication/ Protected routes and authentication with React Router v4
I use react-router-dom v 5.1.2
I initialize keycloak in index.js & pass it as a prop to App.js from it I can get keycloak.idTokenParsed.preferred_username they are: SuperAdmin, Admin, Manager, User. I already have Public routes & Authenticated routes, the part that I am trying to figure out is how to add Role based routes to it? So that only SuperAdmin has access to /create/ & Admin to /edit/. In Routes.jsx I have const for publicRoutes & privateRoutes do I just make 4 for SuperAdmin, Admin, Manager, User or is there a better way to do it?
Routes.jsx*
import Fcontainer from "../containers/Factories/Fcontainer"
import LContainer from "../containers/Lines/LContainer"
import AddLContainer from "../containers/Lines/AddLContainer"
import AddFContainer from "../containers/Factories/AddFContainer"
import LandingPageContainer from "../containers/common/LandingPageContainer"
export const publicRoutes = [
{ path: "/login", type: "public", name: "landing page", component: LandingPageContainer },
]
export const privateRoutes = [
{ path: "/dir/factories", name: "list of factories", component: Fcontainer },
{ path: "/dir/lines", name: "List of lines", component: LContainer },
{ path: "/dir/lines/create", name: "create line", component: AddLContainer },
{ path: "/dir/factories/create", name: "Create factory", component: AddFContainer },
{ path: "/dir/factories/edit/:factoryId", name: "Edit factory", component: AddFContainer },
{ path: "/dir/lines/edit/:lineId", name: "Edit line", component: AddLContainer }
]
App.js
import React, { useEffect } from "react";
import { Switch, withRouter } from "react-router-dom";
import { publicRoutes, privateRoutes } from "../src/routes/Routes";
import { Route, Redirect } from "react-router-dom";
import { connect } from "react-redux";
import { fetchApplicationStyle } from './actions/GetAppStyleAction'
const AppContainer = ({ keycloak }) => {
if(keycloak && keycloak.token) {;
console.log('App.js preferred_username ', keycloak.idTokenParsed.preferred_username);
}
useEffect(() => {
async function fetchStyleApi() {
await fetchApplicationStyle();
if (keycloak && keycloak.authenticated) {
await fetchRoleList()
}
}
fetchStyleApi()
}, [fetchApplicationStyle, fetchRoleList])
return (
<div>
<div>
<ThemeProvider theme={theme}>
{(keycloak && keycloak.token) ?
<React.Fragment>
<Switch>
{privateRoutes.map((prop, key) => {
console.log('App.js Prop & Key ', prop, key)
return (
<Route
path={prop.path}
key={key}
exact={true}
component={prop.component}
/>
);
})}
<Redirect from={'/'} to={'/fcm/factories'} key={'list of factories'} />
</Switch>
</React.Fragment>
:
<React.Fragment>
<Switch>
{publicRoutes.map((prop, key) => {
return (
<Route
path={prop.path}
key={key}
exact={true}
component={(props) =>
<prop.component
keycloak={keycloak}
key={key} {...props} />
}
/>
);
})}
<Redirect from={'/'} to={'/login'} key={'login'} />
</Switch>
</React.Fragment>
}
</ThemeProvider>
</div>
</div>
);
}
const mapStateToProps = state => {
const { roleAndPrivileges, applicationStyleState } = state;
const theme = ThemeCreator(applicationStyleState.appThemeData)
return {
roleAndPrivileges,
theme
}
}
const mapDispatchToProps = { fetchRoleList, fetchApplicationStyle }
const App = connect(
mapStateToProps,
mapDispatchToProps
)(withRouter(AppContainer))
export default App