0

I am using react-redux. I can create as many roles as i need, and a single user can be assigned multiple roles. roles contains the array of resources, each resource contains {canView,canAdd,canUpdate, canDelete} for eg: admin role contains

{id, name, resourceRole: [{canAdd:true, canView:true, canUpdate:true, canDelete: true, resourceId:1 },{canAdd:true, canView:true, canUpdate:true, canDelete: true, resourceId:2}]) 

and so on. Now I am stuck at how to create routing for roles. Is there any way i can acomplish that?

Sushant
  • 33
  • 6

1 Answers1

0

You could simply not render the route if the current user's permissions aren't sufficient?

const currentUser = useSelector(state => state.currentUser);
return (
    <div>
    {currentUser.resourceRole.find(r => r.resourceId === 1).canView && 
         <Route exact path="/resource/1" ... />}
    </div>
);

There are other ways if you want to build something dynamic/generic, but conditional logic like that is the simple approach.

timotgl
  • 2,865
  • 1
  • 9
  • 19
  • But this way it will be long as I have more than 50 resources – Sushant Apr 05 '22 at 15:07
  • You can loop over your list of resources and make this logic dynamic. Just need to map resource ids to route paths, and the corresponding component. – timotgl Apr 06 '22 at 07:55