@react-protected/core 0.2.0 → 0.3.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @react-protected/core
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - cb262d3: loader, action and middleware support
8
+
3
9
  ## 0.2.0
4
10
 
5
11
  ### Minor Changes
package/README.md CHANGED
@@ -1,22 +1,6 @@
1
1
  # @react-protected/core
2
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
- ```
3
+ Framework-agnostic access-control logic. No dependency on React, a router, or redirect policy.
20
4
 
21
5
  ## Usage
22
6
 
@@ -27,36 +11,20 @@ const guard = createGuard({
27
11
  getUser: () => store.getState().user,
28
12
  hasRole: (user, roles) => roles.some((role) => user.roles.includes(role)),
29
13
  hasPermission: (user, permissions) =>
30
- permissions.every((p) => user.permissions.includes(p)),
14
+ permissions.every((permission) => user.permissions.includes(permission)),
31
15
  })
32
16
 
33
17
  const result = guard.check({ access: 'authenticated', roles: ['admin'] })
34
18
 
35
- if (result.allowed) {
36
- // permit access
37
- } else {
38
- // result.reason: 'unauthenticated' | 'forbidden'
19
+ if (!result.allowed) {
20
+ // result.reason: 'unauthenticated' | 'authenticated' | 'forbidden'
39
21
  }
40
22
  ```
41
23
 
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 |
24
+ Supported access levels:
58
25
 
59
- ## Documentation
26
+ - `'public'`
27
+ - `'authenticated'`
28
+ - `'unauthenticated'`
60
29
 
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)
30
+ When `roles` or `permissions` are set without `access`, the guard treats the config as authenticated-only.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function u(r){const s={getUser:r.getUser,isAuthenticated:r.isAuthenticated??(e=>e!==null),hasRole:r.hasRole??(()=>!1),hasPermission:r.hasPermission??(()=>!1)};return{check:e=>{var a,l,n,i;const t=s.getUser(),o=s.isAuthenticated(t);return((e.access??"public")==="authenticated"||!!((a=e.roles)!=null&&a.length)||!!((l=e.permissions)!=null&&l.length))&&!o?{allowed:!1,reason:"unauthenticated"}:(n=e.roles)!=null&&n.length&&t&&!s.hasRole(t,e.roles)?{allowed:!1,reason:"forbidden"}:(i=e.permissions)!=null&&i.length&&t&&!s.hasPermission(t,e.permissions)?{allowed:!1,reason:"forbidden"}:{allowed:!0}},options:s}}exports.createGuard=u;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function c(a){const s={getUser:a.getUser,isAuthenticated:a.isAuthenticated??(e=>e!==null),hasRole:a.hasRole??(()=>!1),hasPermission:a.hasPermission??(()=>!1)};return{check:e=>{var n,o,i,u;const t=s.getUser(),r=s.isAuthenticated(t),l=e.access??"public",d=l==="authenticated"||!!((n=e.roles)!=null&&n.length)||!!((o=e.permissions)!=null&&o.length);return l==="unauthenticated"?r?{allowed:!1,reason:"authenticated"}:{allowed:!0}:d&&!r?{allowed:!1,reason:"unauthenticated"}:(i=e.roles)!=null&&i.length&&t&&!s.hasRole(t,e.roles)?{allowed:!1,reason:"forbidden"}:(u=e.permissions)!=null&&u.length&&t&&!s.hasPermission(t,e.permissions)?{allowed:!1,reason:"forbidden"}:{allowed:!0}},options:s}}exports.createGuard=c;
package/dist/index.js CHANGED
@@ -1,16 +1,16 @@
1
- function d(r) {
1
+ function c(a) {
2
2
  const s = {
3
- getUser: r.getUser,
4
- isAuthenticated: r.isAuthenticated ?? ((e) => e !== null),
5
- hasRole: r.hasRole ?? (() => !1),
6
- hasPermission: r.hasPermission ?? (() => !1)
3
+ getUser: a.getUser,
4
+ isAuthenticated: a.isAuthenticated ?? ((e) => e !== null),
5
+ hasRole: a.hasRole ?? (() => !1),
6
+ hasPermission: a.hasPermission ?? (() => !1)
7
7
  };
8
8
  return { check: (e) => {
9
- var a, n, l, i;
10
- const t = s.getUser(), o = s.isAuthenticated(t);
11
- return ((e.access ?? "public") === "authenticated" || !!((a = e.roles) != null && a.length) || !!((n = e.permissions) != null && n.length)) && !o ? { allowed: !1, reason: "unauthenticated" } : (l = e.roles) != null && l.length && t && !s.hasRole(t, e.roles) ? { allowed: !1, reason: "forbidden" } : (i = e.permissions) != null && i.length && t && !s.hasPermission(t, e.permissions) ? { allowed: !1, reason: "forbidden" } : { allowed: !0 };
9
+ var l, o, i, u;
10
+ const t = s.getUser(), r = s.isAuthenticated(t), n = e.access ?? "public", h = n === "authenticated" || !!((l = e.roles) != null && l.length) || !!((o = e.permissions) != null && o.length);
11
+ return n === "unauthenticated" ? r ? { allowed: !1, reason: "authenticated" } : { allowed: !0 } : h && !r ? { allowed: !1, reason: "unauthenticated" } : (i = e.roles) != null && i.length && t && !s.hasRole(t, e.roles) ? { allowed: !1, reason: "forbidden" } : (u = e.permissions) != null && u.length && t && !s.hasPermission(t, e.permissions) ? { allowed: !1, reason: "forbidden" } : { allowed: !0 };
12
12
  }, options: s };
13
13
  }
14
14
  export {
15
- d as createGuard
15
+ c as createGuard
16
16
  };
package/dist/types.d.ts CHANGED
@@ -1,13 +1,14 @@
1
1
  /**
2
2
  * Access level handled by the framework-agnostic guard.
3
3
  */
4
- export type AccessLevel = 'public' | 'authenticated';
4
+ export type AccessLevel = 'public' | 'authenticated' | 'unauthenticated';
5
5
  /**
6
6
  * Access requirements consumed by `guard.check()` and adapter components.
7
7
  */
8
8
  export type AccessConfig = {
9
9
  /**
10
- * Declares whether access is public or requires an authenticated user.
10
+ * Declares whether access is public, requires an authenticated user, or requires
11
+ * an unauthenticated user.
11
12
  * Defaults to `'public'` when omitted.
12
13
  */
13
14
  access?: AccessLevel;
@@ -32,6 +33,9 @@ export type AccessResult = {
32
33
  } | {
33
34
  allowed: false;
34
35
  reason: 'unauthenticated';
36
+ } | {
37
+ allowed: false;
38
+ reason: 'authenticated';
35
39
  } | {
36
40
  allowed: false;
37
41
  reason: 'forbidden';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-protected/core",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "license": "MIT",
5
5
  "description": "Framework-agnostic route protection logic",
6
6
  "main": "./dist/index.cjs",