@react-protected/react-router 0.1.1-beta.0 → 0.2.0-beta.2

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 ADDED
@@ -0,0 +1,113 @@
1
+ # @react-protected/react-router
2
+
3
+ React Router adapter for [react-protected](https://github.com/astakhovaskold/react-protected). Includes `@react-protected/react` — no need to install both.
4
+
5
+ ---
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @react-protected/react-router
11
+ ```
12
+
13
+ ```bash
14
+ yarn add @react-protected/react-router
15
+ ```
16
+
17
+ ```bash
18
+ pnpm add @react-protected/react-router
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ### Data router (recommended)
24
+
25
+ ```tsx
26
+ import { createAccessRouter } from '@react-protected/react-router'
27
+ import { useAuthStore } from './entities/auth'
28
+
29
+ const router = createAccessRouter(
30
+ [
31
+ { path: '/', element: <HomePage /> },
32
+ { path: '/login', element: <LoginPage />, access: 'guest-only' },
33
+ { path: '/dashboard', element: <DashboardPage />, access: 'authenticated' },
34
+ { path: '/admin', element: <AdminPage />, access: 'authenticated', roles: ['admin'] },
35
+ { path: '/403', element: <Page403 /> },
36
+ ],
37
+ {
38
+ getUser: () => useAuthStore.getState().user,
39
+ hasRole: (user, roles) => roles.some((role) => user.roles.includes(role)),
40
+ loginPath: '/login',
41
+ forbiddenPath: '/403',
42
+ defaultPath: '/dashboard',
43
+ }
44
+ )
45
+
46
+ // App.tsx
47
+ import { RouterProvider } from 'react-router-dom'
48
+ export const App = () => <RouterProvider router={router} />
49
+ ```
50
+
51
+ ### JSX routes
52
+
53
+ ```tsx
54
+ import { Route, Routes } from 'react-router-dom'
55
+ import { AccessProvider, AccessRoute } from '@react-protected/react-router'
56
+
57
+ const App = () => (
58
+ <AccessProvider
59
+ getUser={() => useAuthStore.getState().user}
60
+ hasRole={(user, roles) => roles.some((role) => user.roles.includes(role))}
61
+ loginPath="/login"
62
+ forbiddenPath="/403"
63
+ defaultPath="/dashboard"
64
+ >
65
+ <Routes>
66
+ <Route path="/" element={<HomePage />} />
67
+ <Route
68
+ path="/login"
69
+ element={
70
+ <AccessRoute access="guest-only">
71
+ <LoginPage />
72
+ </AccessRoute>
73
+ }
74
+ />
75
+ <Route
76
+ path="/dashboard"
77
+ element={
78
+ <AccessRoute access="authenticated">
79
+ <DashboardPage />
80
+ </AccessRoute>
81
+ }
82
+ />
83
+ </Routes>
84
+ </AccessProvider>
85
+ )
86
+ ```
87
+
88
+ ### Guarding UI elements
89
+
90
+ ```tsx
91
+ import { HasAccess } from '@react-protected/react-router'
92
+
93
+ const Toolbar = () => (
94
+ <nav>
95
+ <HasAccess roles={['admin']}>
96
+ <button>Delete</button>
97
+ </HasAccess>
98
+ </nav>
99
+ )
100
+ ```
101
+
102
+ ## Packages
103
+
104
+ | Package | Description |
105
+ | --- | --- |
106
+ | `@react-protected/core` | Pure access-control logic — no React, no router |
107
+ | `@react-protected/react` | React context, hooks, and `HasAccess` component |
108
+ | `@react-protected/react-router` | This package — adapter for React Router |
109
+
110
+ ## Documentation
111
+
112
+ - [React Router API](https://github.com/astakhovaskold/react-protected/blob/main/docs/en/api/react-router.md)
113
+ - [Examples](https://github.com/astakhovaskold/react-protected/blob/main/docs/en/README.md)
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const K=require("react"),j=require("@react-protected/react"),b=require("react-router-dom"),ae=require("@react-protected/core");var q={exports:{}},N={};/**
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const ee=require("react"),y=require("@react-protected/react"),m=require("react-router-dom"),ae=require("@react-protected/core");var G={exports:{}},N={};/**
2
2
  * @license React
3
3
  * react-jsx-runtime.production.js
4
4
  *
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * This source code is licensed under the MIT license found in the
8
8
  * LICENSE file in the root directory of this source tree.
9
- */var Z;function se(){if(Z)return N;Z=1;var r=Symbol.for("react.transitional.element"),n=Symbol.for("react.fragment");function s(u,a,o){var i=null;if(o!==void 0&&(i=""+o),a.key!==void 0&&(i=""+a.key),"key"in a){o={};for(var m in a)m!=="key"&&(o[m]=a[m])}else o=a;return a=o.ref,{$$typeof:r,type:u,key:i,ref:a!==void 0?a:null,props:o}}return N.Fragment=n,N.jsx=s,N.jsxs=s,N}var $={};/**
9
+ */var Q;function se(){if(Q)return N;Q=1;var r=Symbol.for("react.transitional.element"),n=Symbol.for("react.fragment");function u(c,a,o){var s=null;if(o!==void 0&&(s=""+o),a.key!==void 0&&(s=""+a.key),"key"in a){o={};for(var d in a)d!=="key"&&(o[d]=a[d])}else o=a;return a=o.ref,{$$typeof:r,type:c,key:s,ref:a!==void 0?a:null,props:o}}return N.Fragment=n,N.jsx=u,N.jsxs=u,N}var C={};/**
10
10
  * @license React
11
11
  * react-jsx-runtime.development.js
12
12
  *
@@ -14,9 +14,9 @@
14
14
  *
15
15
  * This source code is licensed under the MIT license found in the
16
16
  * LICENSE file in the root directory of this source tree.
17
- */var Q;function ue(){return Q||(Q=1,process.env.NODE_ENV!=="production"&&function(){function r(e){if(e==null)return null;if(typeof e=="function")return e.$$typeof===te?null:e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case g:return"Fragment";case y:return"Profiler";case T:return"StrictMode";case x:return"Suspense";case W:return"SuspenseList";case re:return"Activity"}if(typeof e=="object")switch(typeof e.tag=="number"&&console.error("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),e.$$typeof){case k:return"Portal";case U:return e.displayName||"Context";case C:return(e._context.displayName||"Context")+".Consumer";case w:var t=e.render;return e=e.displayName,e||(e=t.displayName||t.name||"",e=e!==""?"ForwardRef("+e+")":"ForwardRef"),e;case A:return t=e.displayName||null,t!==null?t:r(e.type)||"Memo";case S:t=e._payload,e=e._init;try{return r(e(t))}catch{}}return null}function n(e){return""+e}function s(e){try{n(e);var t=!1}catch{t=!0}if(t){t=console;var c=t.error,l=typeof Symbol=="function"&&Symbol.toStringTag&&e[Symbol.toStringTag]||e.constructor.name||"Object";return c.call(t,"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",l),n(e)}}function u(e){if(e===g)return"<>";if(typeof e=="object"&&e!==null&&e.$$typeof===S)return"<...>";try{var t=r(e);return t?"<"+t+">":"<...>"}catch{return"<...>"}}function a(){var e=L.A;return e===null?null:e.getOwner()}function o(){return Error("react-stack-top-frame")}function i(e){if(z.call(e,"key")){var t=Object.getOwnPropertyDescriptor(e,"key").get;if(t&&t.isReactWarning)return!1}return e.key!==void 0}function m(e,t){function c(){V||(V=!0,console.error("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",t))}c.isReactWarning=!0,Object.defineProperty(e,"key",{get:c,configurable:!0})}function P(){var e=r(this.type);return H[e]||(H[e]=!0,console.error("Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release.")),e=this.props.ref,e!==void 0?e:null}function p(e,t,c,l,Y,F){var f=c.ref;return e={$$typeof:_,type:e,key:t,props:c,_owner:l},(f!==void 0?f:null)!==null?Object.defineProperty(e,"ref",{enumerable:!1,get:P}):Object.defineProperty(e,"ref",{enumerable:!1,value:null}),e._store={},Object.defineProperty(e._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:0}),Object.defineProperty(e,"_debugInfo",{configurable:!1,enumerable:!1,writable:!0,value:null}),Object.defineProperty(e,"_debugStack",{configurable:!1,enumerable:!1,writable:!0,value:Y}),Object.defineProperty(e,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:F}),Object.freeze&&(Object.freeze(e.props),Object.freeze(e)),e}function E(e,t,c,l,Y,F){var f=t.children;if(f!==void 0)if(l)if(ne(f)){for(l=0;l<f.length;l++)h(f[l]);Object.freeze&&Object.freeze(f)}else console.error("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else h(f);if(z.call(t,"key")){f=r(e);var O=Object.keys(t).filter(function(oe){return oe!=="key"});l=0<O.length?"{key: someKey, "+O.join(": ..., ")+": ...}":"{key: someKey}",X[f+l]||(O=0<O.length?"{"+O.join(": ..., ")+": ...}":"{}",console.error(`A props object containing a "key" prop is being spread into JSX:
17
+ */var K;function ue(){return K||(K=1,process.env.NODE_ENV!=="production"&&function(){function r(e){if(e==null)return null;if(typeof e=="function")return e.$$typeof===te?null:e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case T:return"Fragment";case O:return"Profiler";case k:return"StrictMode";case S:return"Suspense";case x:return"SuspenseList";case L:return"Activity"}if(typeof e=="object")switch(typeof e.tag=="number"&&console.error("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),e.$$typeof){case R:return"Portal";case $:return e.displayName||"Context";case w:return(e._context.displayName||"Context")+".Consumer";case U:var t=e.render;return e=e.displayName,e||(e=t.displayName||t.name||"",e=e!==""?"ForwardRef("+e+")":"ForwardRef"),e;case z:return t=e.displayName||null,t!==null?t:r(e.type)||"Memo";case A:t=e._payload,e=e._init;try{return r(e(t))}catch{}}return null}function n(e){return""+e}function u(e){try{n(e);var t=!1}catch{t=!0}if(t){t=console;var i=t.error,l=typeof Symbol=="function"&&Symbol.toStringTag&&e[Symbol.toStringTag]||e.constructor.name||"Object";return i.call(t,"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",l),n(e)}}function c(e){if(e===T)return"<>";if(typeof e=="object"&&e!==null&&e.$$typeof===A)return"<...>";try{var t=r(e);return t?"<"+t+">":"<...>"}catch{return"<...>"}}function a(){var e=D.A;return e===null?null:e.getOwner()}function o(){return Error("react-stack-top-frame")}function s(e){if(V.call(e,"key")){var t=Object.getOwnPropertyDescriptor(e,"key").get;if(t&&t.isReactWarning)return!1}return e.key!==void 0}function d(e,t){function i(){H||(H=!0,console.error("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",t))}i.isReactWarning=!0,Object.defineProperty(e,"key",{get:i,configurable:!0})}function g(){var e=r(this.type);return J[e]||(J[e]=!0,console.error("Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release.")),e=this.props.ref,e!==void 0?e:null}function b(e,t,i,l,Y,M){var f=i.ref;return e={$$typeof:h,type:e,key:t,props:i,_owner:l},(f!==void 0?f:null)!==null?Object.defineProperty(e,"ref",{enumerable:!1,get:g}):Object.defineProperty(e,"ref",{enumerable:!1,value:null}),e._store={},Object.defineProperty(e._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:0}),Object.defineProperty(e,"_debugInfo",{configurable:!1,enumerable:!1,writable:!0,value:null}),Object.defineProperty(e,"_debugStack",{configurable:!1,enumerable:!1,writable:!0,value:Y}),Object.defineProperty(e,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:M}),Object.freeze&&(Object.freeze(e.props),Object.freeze(e)),e}function v(e,t,i,l,Y,M){var f=t.children;if(f!==void 0)if(l)if(ne(f)){for(l=0;l<f.length;l++)P(f[l]);Object.freeze&&Object.freeze(f)}else console.error("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else P(f);if(V.call(t,"key")){f=r(e);var j=Object.keys(t).filter(function(oe){return oe!=="key"});l=0<j.length?"{key: someKey, "+j.join(": ..., ")+": ...}":"{key: someKey}",Z[f+l]||(j=0<j.length?"{"+j.join(": ..., ")+": ...}":"{}",console.error(`A props object containing a "key" prop is being spread into JSX:
18
18
  let props = %s;
19
19
  <%s {...props} />
20
20
  React keys must be passed directly to JSX without using spread:
21
21
  let props = %s;
22
- <%s key={someKey} {...props} />`,l,f,O,f),X[f+l]=!0)}if(f=null,c!==void 0&&(s(c),f=""+c),i(t)&&(s(t.key),f=""+t.key),"key"in t){c={};for(var M in t)M!=="key"&&(c[M]=t[M])}else c=t;return f&&m(c,typeof e=="function"?e.displayName||e.name||"Unknown":e),p(e,f,c,a(),Y,F)}function h(e){R(e)?e._store&&(e._store.validated=1):typeof e=="object"&&e!==null&&e.$$typeof===S&&(e._payload.status==="fulfilled"?R(e._payload.value)&&e._payload.value._store&&(e._payload.value._store.validated=1):e._store&&(e._store.validated=1))}function R(e){return typeof e=="object"&&e!==null&&e.$$typeof===_}var d=K,_=Symbol.for("react.transitional.element"),k=Symbol.for("react.portal"),g=Symbol.for("react.fragment"),T=Symbol.for("react.strict_mode"),y=Symbol.for("react.profiler"),C=Symbol.for("react.consumer"),U=Symbol.for("react.context"),w=Symbol.for("react.forward_ref"),x=Symbol.for("react.suspense"),W=Symbol.for("react.suspense_list"),A=Symbol.for("react.memo"),S=Symbol.for("react.lazy"),re=Symbol.for("react.activity"),te=Symbol.for("react.client.reference"),L=d.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,z=Object.prototype.hasOwnProperty,ne=Array.isArray,D=console.createTask?console.createTask:function(){return null};d={react_stack_bottom_frame:function(e){return e()}};var V,H={},J=d.react_stack_bottom_frame.bind(d,o)(),B=D(u(o)),X={};$.Fragment=g,$.jsx=function(e,t,c){var l=1e4>L.recentlyCreatedOwnerStacks++;return E(e,t,c,!1,l?Error("react-stack-top-frame"):J,l?D(u(e)):B)},$.jsxs=function(e,t,c){var l=1e4>L.recentlyCreatedOwnerStacks++;return E(e,t,c,!0,l?Error("react-stack-top-frame"):J,l?D(u(e)):B)}}()),$}process.env.NODE_ENV==="production"?q.exports=se():q.exports=ue();var v=q.exports;function ce(r){const{guard:n}=j.useAccess();return n.check(r)}const ie=K.memo(({access:r,roles:n,permissions:s,meta:u,children:a})=>{const{guard:o,loginPath:i,forbiddenPath:m,defaultPath:P,callbackUrlParam:p}=j.useAccess(),E=b.useLocation();if(r==="guest-only"){const R=o.options.getUser();return o.options.isAuthenticated(R)?v.jsx(b.Navigate,{to:P,replace:!0}):a??v.jsx(b.Outlet,{})}const h=o.check({access:r,roles:n,permissions:s,meta:u});if(!h.allowed){if(h.reason==="unauthenticated"){const R=`${E.pathname}${E.search}${E.hash}`,d=p?`${i}?${p}=${encodeURIComponent(R)}`:i;return v.jsx(b.Navigate,{to:d,replace:!0})}return v.jsx(b.Navigate,{to:m,replace:!0})}return a??v.jsx(b.Outlet,{})});function ee(r,n,s,u,a,o){return r==="unauthenticated"?o?`${s}?${o}=${encodeURIComponent(n)}`:s:r==="forbidden"?u:a}function le({access:r,roles:n,permissions:s,meta:u,guard:a,routeElement:o,RouteComponent:i,loginPath:m,forbiddenPath:P,defaultPath:p,callbackUrlParam:E}){const h=b.useLocation(),R=`${h.pathname}${h.search}${h.hash}`;if(r==="guest-only"){const _=a.options.getUser();return a.options.isAuthenticated(_)?v.jsx(b.Navigate,{to:p,replace:!0}):i?v.jsx(i,{}):o??v.jsx(b.Outlet,{})}const d=a.check({access:r,roles:n,permissions:s,meta:u});if(!d.allowed){const _=ee(d.reason,R,m,P,p,E);return v.jsx(b.Navigate,{to:_,replace:!0})}return i?v.jsx(i,{}):o??v.jsx(b.Outlet,{})}function G(r,n,s){return v.jsx(le,{access:r.protection.access,roles:r.protection.roles,permissions:r.protection.permissions,meta:r.protection.meta,guard:r.guard,routeElement:n,RouteComponent:s??null,loginPath:r.loginPath,forbiddenPath:r.forbiddenPath,defaultPath:r.defaultPath,callbackUrlParam:r.callbackUrlParam})}function I(r,n){return r===void 0||typeof r=="boolean"?r:s=>{const u=new URL(s.request.url),a=`${u.pathname}${u.search}`;if(n.protection.access==="guest-only"){const i=n.guard.options.getUser();return n.guard.options.isAuthenticated(i)?b.redirect(n.defaultPath):r(s)}const o=n.guard.check(n.protection);if(!o.allowed){const i=ee(o.reason,a,n.loginPath,n.forbiddenPath,n.defaultPath,n.callbackUrlParam);return b.redirect(i)}return r(s)}}async function fe(r){const n=await Promise.all(Object.entries(r).map(async([s,u])=>[s,await(u==null?void 0:u())]));return Object.fromEntries(n)}function de(r,n){if(!r)return;const s=typeof r=="function"?r:()=>fe(r);return async()=>{const u=await s();if(!u)return n.hasStaticUi?{}:{element:G(n)};const{element:a,Component:o,loader:i,action:m,...P}=u,p={...P,loader:I(i,n),action:I(m,n)};return n.hasStaticUi||(p.element=G(n,a,o??null)),p}}function me(r,n,s){const{loginPath:u="/login",forbiddenPath:a="/403",defaultPath:o="/",callbackUrlParam:i,...m}=n,P=ae.createGuard(m),p=E=>E.map(h=>{const{access:R,roles:d,permissions:_,meta:k,children:g,element:T,Component:y,loader:C,action:U,lazy:w,...x}=h;if(!(R!==void 0||!!(d!=null&&d.length)||!!(_!=null&&_.length)||k!==void 0))return{...x,element:T,Component:y,loader:C,action:U,lazy:w,children:g?p(g):void 0};const A={guard:P,protection:{access:R,roles:d,permissions:_,meta:k},hasStaticUi:T!=null||y!=null,loginPath:u,forbiddenPath:a,defaultPath:o,callbackUrlParam:i},S=A.hasStaticUi||!w?G(A,T,y??null):void 0;return{...x,element:S,Component:void 0,loader:I(C,A),action:I(U,A),lazy:de(w,A),children:g?p(g):void 0}});return b.createBrowserRouter(p(r),s)}Object.defineProperty(exports,"AccessProvider",{enumerable:!0,get:()=>j.AccessProvider});Object.defineProperty(exports,"HasAccess",{enumerable:!0,get:()=>j.HasAccess});Object.defineProperty(exports,"useAccess",{enumerable:!0,get:()=>j.useAccess});Object.defineProperty(exports,"useHasAccess",{enumerable:!0,get:()=>j.useHasAccess});exports.AccessRoute=ie;exports.createAccessRouter=me;exports.useRouteAccess=ce;
22
+ <%s key={someKey} {...props} />`,l,f,j,f),Z[f+l]=!0)}if(f=null,i!==void 0&&(u(i),f=""+i),s(t)&&(u(t.key),f=""+t.key),"key"in t){i={};for(var q in t)q!=="key"&&(i[q]=t[q])}else i=t;return f&&d(i,typeof e=="function"?e.displayName||e.name||"Unknown":e),b(e,f,i,a(),Y,M)}function P(e){E(e)?e._store&&(e._store.validated=1):typeof e=="object"&&e!==null&&e.$$typeof===A&&(e._payload.status==="fulfilled"?E(e._payload.value)&&e._payload.value._store&&(e._payload.value._store.validated=1):e._store&&(e._store.validated=1))}function E(e){return typeof e=="object"&&e!==null&&e.$$typeof===h}var p=ee,h=Symbol.for("react.transitional.element"),R=Symbol.for("react.portal"),T=Symbol.for("react.fragment"),k=Symbol.for("react.strict_mode"),O=Symbol.for("react.profiler"),w=Symbol.for("react.consumer"),$=Symbol.for("react.context"),U=Symbol.for("react.forward_ref"),S=Symbol.for("react.suspense"),x=Symbol.for("react.suspense_list"),z=Symbol.for("react.memo"),A=Symbol.for("react.lazy"),L=Symbol.for("react.activity"),te=Symbol.for("react.client.reference"),D=p.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,V=Object.prototype.hasOwnProperty,ne=Array.isArray,F=console.createTask?console.createTask:function(){return null};p={react_stack_bottom_frame:function(e){return e()}};var H,J={},B=p.react_stack_bottom_frame.bind(p,o)(),X=F(c(o)),Z={};C.Fragment=T,C.jsx=function(e,t,i){var l=1e4>D.recentlyCreatedOwnerStacks++;return v(e,t,i,!1,l?Error("react-stack-top-frame"):B,l?F(c(e)):X)},C.jsxs=function(e,t,i){var l=1e4>D.recentlyCreatedOwnerStacks++;return v(e,t,i,!0,l?Error("react-stack-top-frame"):B,l?F(c(e)):X)}}()),C}process.env.NODE_ENV==="production"?G.exports=se():G.exports=ue();var _=G.exports;function ce(r){const{guard:n}=y.useAccess();return n.check(r)}const ie=ee.memo(({access:r,roles:n,permissions:u,meta:c,children:a})=>{const{guard:o,loginPath:s,forbiddenPath:d,defaultPath:g,callbackUrlParam:b,shouldAddCallbackUrl:v}=y.useAccess(),P=m.useLocation();if(r==="guest-only"){const p=o.options.getUser();return o.options.isAuthenticated(p)?_.jsx(m.Navigate,{to:g,replace:!0}):a??_.jsx(m.Outlet,{})}const E=o.check({access:r,roles:n,permissions:u,meta:c});if(!E.allowed){if(E.reason==="unauthenticated"){const p=`${P.pathname}${P.search}${P.hash}`,R=b&&((v==null?void 0:v())??!0)?`${s}?${b}=${encodeURIComponent(p)}`:s;return _.jsx(m.Navigate,{to:R,replace:!0})}return _.jsx(m.Navigate,{to:d,replace:!0})}return a??_.jsx(m.Outlet,{})});function re(r,n,u,c,a,o,s){return r==="unauthenticated"?o&&((s==null?void 0:s())??!0)?`${u}?${o}=${encodeURIComponent(n)}`:u:r==="forbidden"?c:a}function le({access:r,roles:n,permissions:u,meta:c,guard:a,routeElement:o,RouteComponent:s,loginPath:d,forbiddenPath:g,defaultPath:b,callbackUrlParam:v,shouldAddCallbackUrl:P}){const E=m.useLocation(),p=`${E.pathname}${E.search}${E.hash}`;if(r==="guest-only"){const R=a.options.getUser();return a.options.isAuthenticated(R)?_.jsx(m.Navigate,{to:b,replace:!0}):s?_.jsx(s,{}):o??_.jsx(m.Outlet,{})}const h=a.check({access:r,roles:n,permissions:u,meta:c});if(!h.allowed){const R=re(h.reason,p,d,g,b,v,P);return _.jsx(m.Navigate,{to:R,replace:!0})}return s?_.jsx(s,{}):o??_.jsx(m.Outlet,{})}function W(r,n,u){return _.jsx(le,{access:r.protection.access,roles:r.protection.roles,permissions:r.protection.permissions,meta:r.protection.meta,guard:r.guard,routeElement:n,RouteComponent:u??null,loginPath:r.loginPath,forbiddenPath:r.forbiddenPath,defaultPath:r.defaultPath,callbackUrlParam:r.callbackUrlParam,shouldAddCallbackUrl:r.shouldAddCallbackUrl})}function I(r,n){return r===void 0||typeof r=="boolean"?r:u=>{const c=new URL(u.request.url),a=`${c.pathname}${c.search}`;if(n.protection.access==="guest-only"){const s=n.guard.options.getUser();return n.guard.options.isAuthenticated(s)?m.redirect(n.defaultPath):r(u)}const o=n.guard.check(n.protection);if(!o.allowed){const s=re(o.reason,a,n.loginPath,n.forbiddenPath,n.defaultPath,n.callbackUrlParam,n.shouldAddCallbackUrl);return m.redirect(s)}return r(u)}}async function fe(r){const n=await Promise.all(Object.entries(r).map(async([u,c])=>[u,await(c==null?void 0:c())]));return Object.fromEntries(n)}function de(r,n){if(!r)return;const u=typeof r=="function"?r:()=>fe(r);return async()=>{const c=await u();if(!c)return n.hasStaticUi?{}:{element:W(n)};const{element:a,Component:o,loader:s,action:d,...g}=c,b={...g,loader:I(s,n),action:I(d,n)};return n.hasStaticUi||(b.element=W(n,a,o??null)),b}}function me(r,n,u){const{loginPath:c="/login",forbiddenPath:a="/403",defaultPath:o="/",callbackUrlParam:s,shouldAddCallbackUrl:d,...g}=n,b=ae.createGuard(g),v=P=>P.map(E=>{const{access:p,roles:h,permissions:R,meta:T,children:k,element:O,Component:w,loader:$,action:U,lazy:S,...x}=E;if(!(p!==void 0||!!(h!=null&&h.length)||!!(R!=null&&R.length)||T!==void 0))return{...x,element:O,Component:w,loader:$,action:U,lazy:S,children:k?v(k):void 0};const A={guard:b,protection:{access:p,roles:h,permissions:R,meta:T},hasStaticUi:O!=null||w!=null,loginPath:c,forbiddenPath:a,defaultPath:o,callbackUrlParam:s,shouldAddCallbackUrl:d},L=A.hasStaticUi||!S?W(A,O,w??null):void 0;return{...x,element:L,Component:void 0,loader:I($,A),action:I(U,A),lazy:de(S,A),children:k?v(k):void 0}});return m.createBrowserRouter(v(r),u)}Object.defineProperty(exports,"AccessProvider",{enumerable:!0,get:()=>y.AccessProvider});Object.defineProperty(exports,"HasAccess",{enumerable:!0,get:()=>y.HasAccess});Object.defineProperty(exports,"useAccess",{enumerable:!0,get:()=>y.useAccess});Object.defineProperty(exports,"useHasAccess",{enumerable:!0,get:()=>y.useHasAccess});exports.AccessRoute=ie;exports.createAccessRouter=me;exports.useRouteAccess=ce;
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import ue, { memo as ce } from "react";
2
- import { useAccess as ee } from "@react-protected/react";
3
- import { AccessProvider as ke, HasAccess as je, useAccess as we, useHasAccess as Oe } from "@react-protected/react";
4
- import { useLocation as re, Navigate as $, Outlet as Y, createBrowserRouter as ie, redirect as Z } from "react-router-dom";
2
+ import { useAccess as re } from "@react-protected/react";
3
+ import { AccessProvider as ge, HasAccess as je, useAccess as we, useHasAccess as Oe } from "@react-protected/react";
4
+ import { useLocation as te, Navigate as C, Outlet as Y, createBrowserRouter as ie, redirect as Q } from "react-router-dom";
5
5
  import { createGuard as le } from "@react-protected/core";
6
- var M = { exports: {} }, y = {};
6
+ var W = { exports: {} }, y = {};
7
7
  /**
8
8
  * @license React
9
9
  * react-jsx-runtime.production.js
@@ -13,27 +13,27 @@ var M = { exports: {} }, y = {};
13
13
  * This source code is licensed under the MIT license found in the
14
14
  * LICENSE file in the root directory of this source tree.
15
15
  */
16
- var Q;
16
+ var K;
17
17
  function fe() {
18
- if (Q) return y;
19
- Q = 1;
18
+ if (K) return y;
19
+ K = 1;
20
20
  var r = Symbol.for("react.transitional.element"), n = Symbol.for("react.fragment");
21
- function s(u, a, o) {
22
- var i = null;
23
- if (o !== void 0 && (i = "" + o), a.key !== void 0 && (i = "" + a.key), "key" in a) {
21
+ function u(c, a, o) {
22
+ var s = null;
23
+ if (o !== void 0 && (s = "" + o), a.key !== void 0 && (s = "" + a.key), "key" in a) {
24
24
  o = {};
25
- for (var m in a)
26
- m !== "key" && (o[m] = a[m]);
25
+ for (var d in a)
26
+ d !== "key" && (o[d] = a[d]);
27
27
  } else o = a;
28
28
  return a = o.ref, {
29
29
  $$typeof: r,
30
- type: u,
31
- key: i,
30
+ type: c,
31
+ key: s,
32
32
  ref: a !== void 0 ? a : null,
33
33
  props: o
34
34
  };
35
35
  }
36
- return y.Fragment = n, y.jsx = s, y.jsxs = s, y;
36
+ return y.Fragment = n, y.jsx = u, y.jsxs = u, y;
37
37
  }
38
38
  var S = {};
39
39
  /**
@@ -45,44 +45,44 @@ var S = {};
45
45
  * This source code is licensed under the MIT license found in the
46
46
  * LICENSE file in the root directory of this source tree.
47
47
  */
48
- var K;
48
+ var ee;
49
49
  function de() {
50
- return K || (K = 1, process.env.NODE_ENV !== "production" && function() {
50
+ return ee || (ee = 1, process.env.NODE_ENV !== "production" && function() {
51
51
  function r(e) {
52
52
  if (e == null) return null;
53
53
  if (typeof e == "function")
54
54
  return e.$$typeof === oe ? null : e.displayName || e.name || null;
55
55
  if (typeof e == "string") return e;
56
56
  switch (e) {
57
- case P:
57
+ case k:
58
58
  return "Fragment";
59
- case j:
59
+ case g:
60
60
  return "Profiler";
61
61
  case A:
62
62
  return "StrictMode";
63
- case x:
63
+ case O:
64
64
  return "Suspense";
65
- case z:
65
+ case U:
66
66
  return "SuspenseList";
67
- case ne:
67
+ case F:
68
68
  return "Activity";
69
69
  }
70
70
  if (typeof e == "object")
71
71
  switch (typeof e.tag == "number" && console.error(
72
72
  "Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."
73
73
  ), e.$$typeof) {
74
- case k:
74
+ case v:
75
75
  return "Portal";
76
- case C:
76
+ case $:
77
77
  return e.displayName || "Context";
78
- case N:
79
- return (e._context.displayName || "Context") + ".Consumer";
80
78
  case w:
79
+ return (e._context.displayName || "Context") + ".Consumer";
80
+ case N:
81
81
  var t = e.render;
82
82
  return e = e.displayName, e || (e = t.displayName || t.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e;
83
- case T:
83
+ case V:
84
84
  return t = e.displayName || null, t !== null ? t : r(e.type) || "Memo";
85
- case O:
85
+ case T:
86
86
  t = e._payload, e = e._init;
87
87
  try {
88
88
  return r(e(t));
@@ -94,7 +94,7 @@ function de() {
94
94
  function n(e) {
95
95
  return "" + e;
96
96
  }
97
- function s(e) {
97
+ function u(e) {
98
98
  try {
99
99
  n(e);
100
100
  var t = !1;
@@ -103,17 +103,17 @@ function de() {
103
103
  }
104
104
  if (t) {
105
105
  t = console;
106
- var c = t.error, l = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object";
107
- return c.call(
106
+ var i = t.error, l = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object";
107
+ return i.call(
108
108
  t,
109
109
  "The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",
110
110
  l
111
111
  ), n(e);
112
112
  }
113
113
  }
114
- function u(e) {
115
- if (e === P) return "<>";
116
- if (typeof e == "object" && e !== null && e.$$typeof === O)
114
+ function c(e) {
115
+ if (e === k) return "<>";
116
+ if (typeof e == "object" && e !== null && e.$$typeof === T)
117
117
  return "<...>";
118
118
  try {
119
119
  var t = r(e);
@@ -123,48 +123,48 @@ function de() {
123
123
  }
124
124
  }
125
125
  function a() {
126
- var e = F.A;
126
+ var e = L.A;
127
127
  return e === null ? null : e.getOwner();
128
128
  }
129
129
  function o() {
130
130
  return Error("react-stack-top-frame");
131
131
  }
132
- function i(e) {
133
- if (V.call(e, "key")) {
132
+ function s(e) {
133
+ if (q.call(e, "key")) {
134
134
  var t = Object.getOwnPropertyDescriptor(e, "key").get;
135
135
  if (t && t.isReactWarning) return !1;
136
136
  }
137
137
  return e.key !== void 0;
138
138
  }
139
- function m(e, t) {
140
- function c() {
141
- q || (q = !0, console.error(
139
+ function d(e, t) {
140
+ function i() {
141
+ J || (J = !0, console.error(
142
142
  "%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",
143
143
  t
144
144
  ));
145
145
  }
146
- c.isReactWarning = !0, Object.defineProperty(e, "key", {
147
- get: c,
146
+ i.isReactWarning = !0, Object.defineProperty(e, "key", {
147
+ get: i,
148
148
  configurable: !0
149
149
  });
150
150
  }
151
- function R() {
151
+ function P() {
152
152
  var e = r(this.type);
153
- return J[e] || (J[e] = !0, console.error(
153
+ return B[e] || (B[e] = !0, console.error(
154
154
  "Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release."
155
155
  )), e = this.props.ref, e !== void 0 ? e : null;
156
156
  }
157
- function p(e, t, c, l, U, D) {
158
- var f = c.ref;
157
+ function p(e, t, i, l, x, G) {
158
+ var f = i.ref;
159
159
  return e = {
160
- $$typeof: _,
160
+ $$typeof: h,
161
161
  type: e,
162
162
  key: t,
163
- props: c,
163
+ props: i,
164
164
  _owner: l
165
165
  }, (f !== void 0 ? f : null) !== null ? Object.defineProperty(e, "ref", {
166
166
  enumerable: !1,
167
- get: R
167
+ get: P
168
168
  }) : Object.defineProperty(e, "ref", { enumerable: !1, value: null }), e._store = {}, Object.defineProperty(e._store, "validated", {
169
169
  configurable: !1,
170
170
  enumerable: !1,
@@ -179,33 +179,33 @@ function de() {
179
179
  configurable: !1,
180
180
  enumerable: !1,
181
181
  writable: !0,
182
- value: U
182
+ value: x
183
183
  }), Object.defineProperty(e, "_debugTask", {
184
184
  configurable: !1,
185
185
  enumerable: !1,
186
186
  writable: !0,
187
- value: D
187
+ value: G
188
188
  }), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e;
189
189
  }
190
- function E(e, t, c, l, U, D) {
190
+ function b(e, t, i, l, x, G) {
191
191
  var f = t.children;
192
192
  if (f !== void 0)
193
193
  if (l)
194
194
  if (ae(f)) {
195
195
  for (l = 0; l < f.length; l++)
196
- b(f[l]);
196
+ R(f[l]);
197
197
  Object.freeze && Object.freeze(f);
198
198
  } else
199
199
  console.error(
200
200
  "React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead."
201
201
  );
202
- else b(f);
203
- if (V.call(t, "key")) {
202
+ else R(f);
203
+ if (q.call(t, "key")) {
204
204
  f = r(e);
205
- var g = Object.keys(t).filter(function(se) {
205
+ var j = Object.keys(t).filter(function(se) {
206
206
  return se !== "key";
207
207
  });
208
- l = 0 < g.length ? "{key: someKey, " + g.join(": ..., ") + ": ...}" : "{key: someKey}", H[f + l] || (g = 0 < g.length ? "{" + g.join(": ..., ") + ": ...}" : "{}", console.error(
208
+ l = 0 < j.length ? "{key: someKey, " + j.join(": ..., ") + ": ...}" : "{key: someKey}", Z[f + l] || (j = 0 < j.length ? "{" + j.join(": ..., ") + ": ...}" : "{}", console.error(
209
209
  `A props object containing a "key" prop is being spread into JSX:
210
210
  let props = %s;
211
211
  <%s {...props} />
@@ -214,133 +214,135 @@ React keys must be passed directly to JSX without using spread:
214
214
  <%s key={someKey} {...props} />`,
215
215
  l,
216
216
  f,
217
- g,
217
+ j,
218
218
  f
219
- ), H[f + l] = !0);
219
+ ), Z[f + l] = !0);
220
220
  }
221
- if (f = null, c !== void 0 && (s(c), f = "" + c), i(t) && (s(t.key), f = "" + t.key), "key" in t) {
222
- c = {};
223
- for (var G in t)
224
- G !== "key" && (c[G] = t[G]);
225
- } else c = t;
226
- return f && m(
227
- c,
221
+ if (f = null, i !== void 0 && (u(i), f = "" + i), s(t) && (u(t.key), f = "" + t.key), "key" in t) {
222
+ i = {};
223
+ for (var M in t)
224
+ M !== "key" && (i[M] = t[M]);
225
+ } else i = t;
226
+ return f && d(
227
+ i,
228
228
  typeof e == "function" ? e.displayName || e.name || "Unknown" : e
229
229
  ), p(
230
230
  e,
231
231
  f,
232
- c,
232
+ i,
233
233
  a(),
234
- U,
235
- D
234
+ x,
235
+ G
236
236
  );
237
237
  }
238
- function b(e) {
239
- v(e) ? e._store && (e._store.validated = 1) : typeof e == "object" && e !== null && e.$$typeof === O && (e._payload.status === "fulfilled" ? v(e._payload.value) && e._payload.value._store && (e._payload.value._store.validated = 1) : e._store && (e._store.validated = 1));
238
+ function R(e) {
239
+ E(e) ? e._store && (e._store.validated = 1) : typeof e == "object" && e !== null && e.$$typeof === T && (e._payload.status === "fulfilled" ? E(e._payload.value) && e._payload.value._store && (e._payload.value._store.validated = 1) : e._store && (e._store.validated = 1));
240
240
  }
241
- function v(e) {
242
- return typeof e == "object" && e !== null && e.$$typeof === _;
241
+ function E(e) {
242
+ return typeof e == "object" && e !== null && e.$$typeof === h;
243
243
  }
244
- var d = ue, _ = Symbol.for("react.transitional.element"), k = Symbol.for("react.portal"), P = Symbol.for("react.fragment"), A = Symbol.for("react.strict_mode"), j = Symbol.for("react.profiler"), N = Symbol.for("react.consumer"), C = Symbol.for("react.context"), w = Symbol.for("react.forward_ref"), x = Symbol.for("react.suspense"), z = Symbol.for("react.suspense_list"), T = Symbol.for("react.memo"), O = Symbol.for("react.lazy"), ne = Symbol.for("react.activity"), oe = Symbol.for("react.client.reference"), F = d.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, V = Object.prototype.hasOwnProperty, ae = Array.isArray, L = console.createTask ? console.createTask : function() {
244
+ var m = ue, h = Symbol.for("react.transitional.element"), v = Symbol.for("react.portal"), k = Symbol.for("react.fragment"), A = Symbol.for("react.strict_mode"), g = Symbol.for("react.profiler"), w = Symbol.for("react.consumer"), $ = Symbol.for("react.context"), N = Symbol.for("react.forward_ref"), O = Symbol.for("react.suspense"), U = Symbol.for("react.suspense_list"), V = Symbol.for("react.memo"), T = Symbol.for("react.lazy"), F = Symbol.for("react.activity"), oe = Symbol.for("react.client.reference"), L = m.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, q = Object.prototype.hasOwnProperty, ae = Array.isArray, D = console.createTask ? console.createTask : function() {
245
245
  return null;
246
246
  };
247
- d = {
247
+ m = {
248
248
  react_stack_bottom_frame: function(e) {
249
249
  return e();
250
250
  }
251
251
  };
252
- var q, J = {}, B = d.react_stack_bottom_frame.bind(
253
- d,
252
+ var J, B = {}, X = m.react_stack_bottom_frame.bind(
253
+ m,
254
254
  o
255
- )(), X = L(u(o)), H = {};
256
- S.Fragment = P, S.jsx = function(e, t, c) {
257
- var l = 1e4 > F.recentlyCreatedOwnerStacks++;
258
- return E(
255
+ )(), H = D(c(o)), Z = {};
256
+ S.Fragment = k, S.jsx = function(e, t, i) {
257
+ var l = 1e4 > L.recentlyCreatedOwnerStacks++;
258
+ return b(
259
259
  e,
260
260
  t,
261
- c,
261
+ i,
262
262
  !1,
263
- l ? Error("react-stack-top-frame") : B,
264
- l ? L(u(e)) : X
263
+ l ? Error("react-stack-top-frame") : X,
264
+ l ? D(c(e)) : H
265
265
  );
266
- }, S.jsxs = function(e, t, c) {
267
- var l = 1e4 > F.recentlyCreatedOwnerStacks++;
268
- return E(
266
+ }, S.jsxs = function(e, t, i) {
267
+ var l = 1e4 > L.recentlyCreatedOwnerStacks++;
268
+ return b(
269
269
  e,
270
270
  t,
271
- c,
271
+ i,
272
272
  !0,
273
- l ? Error("react-stack-top-frame") : B,
274
- l ? L(u(e)) : X
273
+ l ? Error("react-stack-top-frame") : X,
274
+ l ? D(c(e)) : H
275
275
  );
276
276
  };
277
277
  }()), S;
278
278
  }
279
- process.env.NODE_ENV === "production" ? M.exports = fe() : M.exports = de();
280
- var h = M.exports;
279
+ process.env.NODE_ENV === "production" ? W.exports = fe() : W.exports = de();
280
+ var _ = W.exports;
281
281
  function Re(r) {
282
- const { guard: n } = ee();
282
+ const { guard: n } = re();
283
283
  return n.check(r);
284
284
  }
285
285
  const Pe = ce(({
286
286
  access: r,
287
287
  roles: n,
288
- permissions: s,
289
- meta: u,
288
+ permissions: u,
289
+ meta: c,
290
290
  children: a
291
291
  }) => {
292
- const { guard: o, loginPath: i, forbiddenPath: m, defaultPath: R, callbackUrlParam: p } = ee(), E = re();
292
+ const { guard: o, loginPath: s, forbiddenPath: d, defaultPath: P, callbackUrlParam: p, shouldAddCallbackUrl: b } = re(), R = te();
293
293
  if (r === "guest-only") {
294
- const v = o.options.getUser();
295
- return o.options.isAuthenticated(v) ? /* @__PURE__ */ h.jsx($, { to: R, replace: !0 }) : a ?? /* @__PURE__ */ h.jsx(Y, {});
294
+ const m = o.options.getUser();
295
+ return o.options.isAuthenticated(m) ? /* @__PURE__ */ _.jsx(C, { to: P, replace: !0 }) : a ?? /* @__PURE__ */ _.jsx(Y, {});
296
296
  }
297
- const b = o.check({ access: r, roles: n, permissions: s, meta: u });
298
- if (!b.allowed) {
299
- if (b.reason === "unauthenticated") {
300
- const v = `${E.pathname}${E.search}${E.hash}`, d = p ? `${i}?${p}=${encodeURIComponent(v)}` : i;
301
- return /* @__PURE__ */ h.jsx($, { to: d, replace: !0 });
297
+ const E = o.check({ access: r, roles: n, permissions: u, meta: c });
298
+ if (!E.allowed) {
299
+ if (E.reason === "unauthenticated") {
300
+ const m = `${R.pathname}${R.search}${R.hash}`, v = p && ((b == null ? void 0 : b()) ?? !0) ? `${s}?${p}=${encodeURIComponent(m)}` : s;
301
+ return /* @__PURE__ */ _.jsx(C, { to: v, replace: !0 });
302
302
  }
303
- return /* @__PURE__ */ h.jsx($, { to: m, replace: !0 });
303
+ return /* @__PURE__ */ _.jsx(C, { to: d, replace: !0 });
304
304
  }
305
- return a ?? /* @__PURE__ */ h.jsx(Y, {});
305
+ return a ?? /* @__PURE__ */ _.jsx(Y, {});
306
306
  });
307
- function te(r, n, s, u, a, o) {
308
- return r === "unauthenticated" ? o ? `${s}?${o}=${encodeURIComponent(n)}` : s : r === "forbidden" ? u : a;
307
+ function ne(r, n, u, c, a, o, s) {
308
+ return r === "unauthenticated" ? o && ((s == null ? void 0 : s()) ?? !0) ? `${u}?${o}=${encodeURIComponent(n)}` : u : r === "forbidden" ? c : a;
309
309
  }
310
310
  function me({
311
311
  access: r,
312
312
  roles: n,
313
- permissions: s,
314
- meta: u,
313
+ permissions: u,
314
+ meta: c,
315
315
  guard: a,
316
316
  routeElement: o,
317
- RouteComponent: i,
318
- loginPath: m,
319
- forbiddenPath: R,
317
+ RouteComponent: s,
318
+ loginPath: d,
319
+ forbiddenPath: P,
320
320
  defaultPath: p,
321
- callbackUrlParam: E
321
+ callbackUrlParam: b,
322
+ shouldAddCallbackUrl: R
322
323
  }) {
323
- const b = re(), v = `${b.pathname}${b.search}${b.hash}`;
324
+ const E = te(), m = `${E.pathname}${E.search}${E.hash}`;
324
325
  if (r === "guest-only") {
325
- const _ = a.options.getUser();
326
- return a.options.isAuthenticated(_) ? /* @__PURE__ */ h.jsx($, { to: p, replace: !0 }) : i ? /* @__PURE__ */ h.jsx(i, {}) : o ?? /* @__PURE__ */ h.jsx(Y, {});
326
+ const v = a.options.getUser();
327
+ return a.options.isAuthenticated(v) ? /* @__PURE__ */ _.jsx(C, { to: p, replace: !0 }) : s ? /* @__PURE__ */ _.jsx(s, {}) : o ?? /* @__PURE__ */ _.jsx(Y, {});
327
328
  }
328
- const d = a.check({ access: r, roles: n, permissions: s, meta: u });
329
- if (!d.allowed) {
330
- const _ = te(
331
- d.reason,
332
- v,
329
+ const h = a.check({ access: r, roles: n, permissions: u, meta: c });
330
+ if (!h.allowed) {
331
+ const v = ne(
332
+ h.reason,
333
333
  m,
334
- R,
334
+ d,
335
+ P,
335
336
  p,
336
- E
337
+ b,
338
+ R
337
339
  );
338
- return /* @__PURE__ */ h.jsx($, { to: _, replace: !0 });
340
+ return /* @__PURE__ */ _.jsx(C, { to: v, replace: !0 });
339
341
  }
340
- return i ? /* @__PURE__ */ h.jsx(i, {}) : o ?? /* @__PURE__ */ h.jsx(Y, {});
342
+ return s ? /* @__PURE__ */ _.jsx(s, {}) : o ?? /* @__PURE__ */ _.jsx(Y, {});
341
343
  }
342
- function W(r, n, s) {
343
- return /* @__PURE__ */ h.jsx(
344
+ function z(r, n, u) {
345
+ return /* @__PURE__ */ _.jsx(
344
346
  me,
345
347
  {
346
348
  access: r.protection.access,
@@ -349,111 +351,115 @@ function W(r, n, s) {
349
351
  meta: r.protection.meta,
350
352
  guard: r.guard,
351
353
  routeElement: n,
352
- RouteComponent: s ?? null,
354
+ RouteComponent: u ?? null,
353
355
  loginPath: r.loginPath,
354
356
  forbiddenPath: r.forbiddenPath,
355
357
  defaultPath: r.defaultPath,
356
- callbackUrlParam: r.callbackUrlParam
358
+ callbackUrlParam: r.callbackUrlParam,
359
+ shouldAddCallbackUrl: r.shouldAddCallbackUrl
357
360
  }
358
361
  );
359
362
  }
360
363
  function I(r, n) {
361
- return r === void 0 || typeof r == "boolean" ? r : (s) => {
362
- const u = new URL(s.request.url), a = `${u.pathname}${u.search}`;
364
+ return r === void 0 || typeof r == "boolean" ? r : (u) => {
365
+ const c = new URL(u.request.url), a = `${c.pathname}${c.search}`;
363
366
  if (n.protection.access === "guest-only") {
364
- const i = n.guard.options.getUser();
365
- return n.guard.options.isAuthenticated(i) ? Z(n.defaultPath) : r(s);
367
+ const s = n.guard.options.getUser();
368
+ return n.guard.options.isAuthenticated(s) ? Q(n.defaultPath) : r(u);
366
369
  }
367
370
  const o = n.guard.check(n.protection);
368
371
  if (!o.allowed) {
369
- const i = te(
372
+ const s = ne(
370
373
  o.reason,
371
374
  a,
372
375
  n.loginPath,
373
376
  n.forbiddenPath,
374
377
  n.defaultPath,
375
- n.callbackUrlParam
378
+ n.callbackUrlParam,
379
+ n.shouldAddCallbackUrl
376
380
  );
377
- return Z(i);
381
+ return Q(s);
378
382
  }
379
- return r(s);
383
+ return r(u);
380
384
  };
381
385
  }
382
386
  async function pe(r) {
383
387
  const n = await Promise.all(
384
- Object.entries(r).map(async ([s, u]) => [s, await (u == null ? void 0 : u())])
388
+ Object.entries(r).map(async ([u, c]) => [u, await (c == null ? void 0 : c())])
385
389
  );
386
390
  return Object.fromEntries(n);
387
391
  }
388
- function he(r, n) {
392
+ function be(r, n) {
389
393
  if (!r) return;
390
- const s = typeof r == "function" ? r : () => pe(r);
394
+ const u = typeof r == "function" ? r : () => pe(r);
391
395
  return async () => {
392
- const u = await s();
393
- if (!u)
394
- return n.hasStaticUi ? {} : { element: W(n) };
395
- const { element: a, Component: o, loader: i, action: m, ...R } = u, p = {
396
- ...R,
397
- loader: I(i, n),
398
- action: I(m, n)
396
+ const c = await u();
397
+ if (!c)
398
+ return n.hasStaticUi ? {} : { element: z(n) };
399
+ const { element: a, Component: o, loader: s, action: d, ...P } = c, p = {
400
+ ...P,
401
+ loader: I(s, n),
402
+ action: I(d, n)
399
403
  };
400
- return n.hasStaticUi || (p.element = W(n, a, o ?? null)), p;
404
+ return n.hasStaticUi || (p.element = z(n, a, o ?? null)), p;
401
405
  };
402
406
  }
403
- function Te(r, n, s) {
407
+ function Te(r, n, u) {
404
408
  const {
405
- loginPath: u = "/login",
409
+ loginPath: c = "/login",
406
410
  forbiddenPath: a = "/403",
407
411
  defaultPath: o = "/",
408
- callbackUrlParam: i,
409
- ...m
410
- } = n, R = le(m), p = (E) => E.map((b) => {
412
+ callbackUrlParam: s,
413
+ shouldAddCallbackUrl: d,
414
+ ...P
415
+ } = n, p = le(P), b = (R) => R.map((E) => {
411
416
  const {
412
- access: v,
413
- roles: d,
414
- permissions: _,
417
+ access: m,
418
+ roles: h,
419
+ permissions: v,
415
420
  meta: k,
416
- children: P,
417
- element: A,
418
- Component: j,
419
- loader: N,
420
- action: C,
421
- lazy: w,
422
- ...x
423
- } = b;
424
- if (!(v !== void 0 || !!(d != null && d.length) || !!(_ != null && _.length) || k !== void 0))
421
+ children: A,
422
+ element: g,
423
+ Component: w,
424
+ loader: $,
425
+ action: N,
426
+ lazy: O,
427
+ ...U
428
+ } = E;
429
+ if (!(m !== void 0 || !!(h != null && h.length) || !!(v != null && v.length) || k !== void 0))
425
430
  return {
426
- ...x,
427
- element: A,
428
- Component: j,
429
- loader: N,
430
- action: C,
431
- lazy: w,
432
- children: P ? p(P) : void 0
431
+ ...U,
432
+ element: g,
433
+ Component: w,
434
+ loader: $,
435
+ action: N,
436
+ lazy: O,
437
+ children: A ? b(A) : void 0
433
438
  };
434
439
  const T = {
435
- guard: R,
436
- protection: { access: v, roles: d, permissions: _, meta: k },
437
- hasStaticUi: A != null || j != null,
438
- loginPath: u,
440
+ guard: p,
441
+ protection: { access: m, roles: h, permissions: v, meta: k },
442
+ hasStaticUi: g != null || w != null,
443
+ loginPath: c,
439
444
  forbiddenPath: a,
440
445
  defaultPath: o,
441
- callbackUrlParam: i
442
- }, O = T.hasStaticUi || !w ? W(T, A, j ?? null) : void 0;
446
+ callbackUrlParam: s,
447
+ shouldAddCallbackUrl: d
448
+ }, F = T.hasStaticUi || !O ? z(T, g, w ?? null) : void 0;
443
449
  return {
444
- ...x,
445
- element: O,
450
+ ...U,
451
+ element: F,
446
452
  Component: void 0,
447
- loader: I(N, T),
448
- action: I(C, T),
449
- lazy: he(w, T),
450
- children: P ? p(P) : void 0
453
+ loader: I($, T),
454
+ action: I(N, T),
455
+ lazy: be(O, T),
456
+ children: A ? b(A) : void 0
451
457
  };
452
458
  });
453
- return ie(p(r), s);
459
+ return ie(b(r), u);
454
460
  }
455
461
  export {
456
- ke as AccessProvider,
462
+ ge as AccessProvider,
457
463
  Pe as AccessRoute,
458
464
  je as HasAccess,
459
465
  Te as createAccessRouter,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-protected/react-router",
3
- "version": "0.1.1-beta.0",
3
+ "version": "0.2.0-beta.2",
4
4
  "license": "MIT",
5
5
  "description": "React Router data router adapter for react-protected",
6
6
  "main": "./dist/index.cjs",
@@ -25,8 +25,8 @@
25
25
  "dist"
26
26
  ],
27
27
  "dependencies": {
28
- "@react-protected/react": "0.1.1-beta.0",
29
- "@react-protected/core": "0.1.1-beta.0"
28
+ "@react-protected/core": "0.2.0-beta.2",
29
+ "@react-protected/react": "0.2.0-beta.2"
30
30
  },
31
31
  "peerDependencies": {
32
32
  "react": ">=18.0.0",