react-checker-maker
Version:
React checker maker is a lightweight package that can be used to manage privileges in [React Js](https://reactjs.org). This package utilizes [React Router Dom](https://reactrouter.com) and allows for the addition of specific privileges to routes and eleme
120 lines (81 loc) • 2.62 kB
Markdown
React checker maker is a lightweight package that can be used to manage privileges in [React Js](https://reactjs.org). This package utilizes [React Router Dom](https://reactrouter.com) and allows for the addition of specific privileges to routes and elements.
```sh
npm i react-checker-maker
```
```sh
yarn add react-checker-maker
```
```sh
pnpm add react-checker-maker
```
> User privileges refer to the level of permission granted to a user, which can be specific to a particular route or element. When a privilege is assigned at the route or element level, it is compared with the user's assigned privileges. If the user's privileges include the assigned privilege, then they will be granted access, otherwise they will be denied access.
| App Component |
| ------------- |
```sh
import { PrivilegeProvider } from "react-checker-maker"
import { useLocation } from "react-router-dom";
export default function App(){
// useLocation from react-router-dom
const { pathname } = useLocation();
return (
<PrivilegeProvider pathname={pathname}>
<Routes />
</PrivilegeProvider>
)
}
```
| Routes Component |
| ---------------- |
```sh
import { useCheckerMaker } from "react-checker-maker"
export default function Routes(){
const routes =[] //routes is list of RouteObject
const userPrivileges = [] //userPrivileges is list of privileges
const elementPrivileges = {}
return useRoutes(useCheckerMaker({routes,userPrivileges,elementPrivileges}))
}
```
```sh
<button data-priv-id="#create-user">Create User</button>
<button data-priv-id="#edit-user">Edit User</button>
```
**data-priv-id**
```sh
value must be string starts with `
```
**userPrivileges (Required)**
```sh
string | number | Array<string | number>
```
**routes (Required)**
```sh
Existing routes list with added **privileges** of type string | number | Array<string | number>
```
**elementPrivileges (Optional)**
```sh
{
`
}
or
{
`
key: string | number | Array<string | number>,
removeEL?: boolean, //default -> true
css?: cssProperties,
elementProps?: {},
}
}
```
This repository containing the following packages:
- [`react`](/package/react)
- [`react-router-dom`](/package/react-router-dom)