@react-protected/react 0.1.1-beta.0 → 0.1.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +82 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# @react-protected/react
|
|
2
|
+
|
|
3
|
+
React context, hooks, and `HasAccess` component for [react-protected](https://github.com/astakhovaskold/react-protected).
|
|
4
|
+
|
|
5
|
+
> **Using React Router?** Install [`@react-protected/react-router`](https://www.npmjs.com/package/@react-protected/react-router) instead — it includes this package.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @react-protected/core @react-protected/react
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
yarn add @react-protected/core @react-protected/react
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pnpm add @react-protected/core @react-protected/react
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### AccessProvider
|
|
26
|
+
|
|
27
|
+
Wrap your app with `AccessProvider` to provide the guard to the component tree:
|
|
28
|
+
|
|
29
|
+
```tsx
|
|
30
|
+
import { AccessProvider } from '@react-protected/react'
|
|
31
|
+
|
|
32
|
+
const App = () => (
|
|
33
|
+
<AccessProvider
|
|
34
|
+
getUser={() => authStore.user}
|
|
35
|
+
hasRole={(user, roles) => roles.some((role) => user.roles.includes(role))}
|
|
36
|
+
loginPath="/login"
|
|
37
|
+
forbiddenPath="/403"
|
|
38
|
+
defaultPath="/dashboard"
|
|
39
|
+
>
|
|
40
|
+
<Router />
|
|
41
|
+
</AccessProvider>
|
|
42
|
+
)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### HasAccess
|
|
46
|
+
|
|
47
|
+
Conditionally render UI elements based on access:
|
|
48
|
+
|
|
49
|
+
```tsx
|
|
50
|
+
import { HasAccess } from '@react-protected/react'
|
|
51
|
+
|
|
52
|
+
const Toolbar = () => (
|
|
53
|
+
<nav>
|
|
54
|
+
<HasAccess roles={['admin']}>
|
|
55
|
+
<button>Delete</button>
|
|
56
|
+
</HasAccess>
|
|
57
|
+
</nav>
|
|
58
|
+
)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### useHasAccess
|
|
62
|
+
|
|
63
|
+
Hook version of `HasAccess`:
|
|
64
|
+
|
|
65
|
+
```tsx
|
|
66
|
+
import { useHasAccess } from '@react-protected/react'
|
|
67
|
+
|
|
68
|
+
const canDelete = useHasAccess({ roles: ['admin'] })
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Packages
|
|
72
|
+
|
|
73
|
+
| Package | Description |
|
|
74
|
+
| --- | --- |
|
|
75
|
+
| `@react-protected/core` | Pure access-control logic — no React, no router |
|
|
76
|
+
| `@react-protected/react` | This package — React context, hooks, and `HasAccess` |
|
|
77
|
+
| `@react-protected/react-router` | Adapter for React Router |
|
|
78
|
+
|
|
79
|
+
## Documentation
|
|
80
|
+
|
|
81
|
+
- [Full documentation](https://github.com/astakhovaskold/react-protected/blob/main/docs/en/README.md)
|
|
82
|
+
- [Examples](https://github.com/astakhovaskold/react-protected/blob/main/docs/en/examples/basic.md)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-protected/react",
|
|
3
|
-
"version": "0.1.1-beta.
|
|
3
|
+
"version": "0.1.1-beta.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "React context and hooks for react-protected",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"dist"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@react-protected/core": "0.1.1-beta.
|
|
28
|
+
"@react-protected/core": "0.1.1-beta.1"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"react": ">=18.0.0",
|