@react-protected/core 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 +62 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# @react-protected/core
|
|
2
|
+
|
|
3
|
+
Framework-agnostic access-control logic. No dependency on React, a router, or a store.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @react-protected/core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
yarn add @react-protected/core
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm add @react-protected/core
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { createGuard } from '@react-protected/core'
|
|
25
|
+
|
|
26
|
+
const guard = createGuard({
|
|
27
|
+
getUser: () => store.getState().user,
|
|
28
|
+
hasRole: (user, roles) => roles.some((role) => user.roles.includes(role)),
|
|
29
|
+
hasPermission: (user, permissions) =>
|
|
30
|
+
permissions.every((p) => user.permissions.includes(p)),
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
const result = guard.check({ access: 'authenticated', roles: ['admin'] })
|
|
34
|
+
|
|
35
|
+
if (result.allowed) {
|
|
36
|
+
// permit access
|
|
37
|
+
} else {
|
|
38
|
+
// result.reason: 'unauthenticated' | 'forbidden'
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Implicit auth requirement
|
|
43
|
+
|
|
44
|
+
When `roles` or `permissions` are set without an explicit `access` field, the route is treated as `'authenticated'` automatically:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
guard.check({ roles: ['admin'] })
|
|
48
|
+
// equivalent to: guard.check({ access: 'authenticated', roles: ['admin'] })
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Packages
|
|
52
|
+
|
|
53
|
+
| Package | Description |
|
|
54
|
+
| --- | --- |
|
|
55
|
+
| `@react-protected/core` | This package — pure access-control logic |
|
|
56
|
+
| `@react-protected/react` | React context, hooks, and `HasAccess` component |
|
|
57
|
+
| `@react-protected/react-router` | Adapter for React Router |
|
|
58
|
+
|
|
59
|
+
## Documentation
|
|
60
|
+
|
|
61
|
+
- [Core API](https://github.com/astakhovaskold/react-protected/blob/main/docs/en/api/core.md)
|
|
62
|
+
- [Examples](https://github.com/astakhovaskold/react-protected/blob/main/docs/en/README.md)
|