I’m trying to achieve giving user permission using a checkbox list via MERN, I already tried doing a manual user permission by giving fixed permission using an if and else statement. Here is an example:
{user.role == "SuperAdmin" || user.role == "Admin" ? (
<>
{/* Users */}
<PrivateRoute path="/admin/users" component={Users} exact />
<PrivateRoute path="/admin/users/create" component={AddUser} exact />
<PrivateRoute path="/admin/users/edit/:id" component={EditUser} exact />
) : (
<div id="notfound">
<div class="notfound">
<div class="notfound-404">
<h1>Oops!</h1>
</div>
<h2>404 - You are not Authorized</h2>
<p>
The page you are looking for might have been removed had
its name changed or you are not authorized to access this
page.
</p>
<a href="#">Go To Homepage</a>
</div>
</div>
)}
As you can see I used if and else statement in the route, are there any packages tutorial or any sample code you could give me to assign user permission using checkbox list with MERN, thank you all for your help your answers will deeply be appreciated. :)
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Hi there,
Here’s a general guideline for assigning user permissions via a checkbox list:
Here’s a basic example using React:
Here’s an example Express route:
In your React app, when the user logs in, you would fetch their permissions from the server and store them in your React app’s state.
Now, instead of checking the user’s role in your routes, you would check their permissions.
Here’s an example:
And here’s how the
PrivateRoute
component might look:Please note that you need to handle permissions on the server side as well. Even if the front-end doesn’t allow users to navigate to certain routes, users could still manually make requests to your Express server, so you need to make sure that your server checks permissions before responding to a request.
This is just a rough guideline. There are many details that you would need to fill in, depending on the specifics of your application. Please be aware that implementing security features like this can be complex and needs to be done carefully to avoid introducing security vulnerabilities.
Best,
Bobby