@react-protected/react-router 0.1.1-beta.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/LICENSE.md +21 -0
- package/dist/AccessRoute.d.ts +5 -0
- package/dist/createAccessRouter.d.ts +3 -0
- package/dist/index.cjs +22 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +463 -0
- package/dist/types.d.ts +16 -0
- package/package.json +50 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 react-protected contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { AccessResult } from '@react-protected/core';
|
|
2
|
+
import { RouteProtection } from '@react-protected/react';
|
|
3
|
+
import { AccessRouteProps } from './types';
|
|
4
|
+
export declare function useRouteAccess(config: RouteProtection): AccessResult;
|
|
5
|
+
export declare const AccessRoute: import('react').MemoExoticComponent<({ access, roles, permissions, meta, children, }: AccessRouteProps) => string | number | boolean | Iterable<import('react').ReactNode> | import("react/jsx-runtime").JSX.Element>;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { createBrowserRouter } from 'react-router-dom';
|
|
2
|
+
import { CreateAccessRouterConfig, CreateAccessRouterOptions, ProtectedRouteObject } from './types';
|
|
3
|
+
export declare function createAccessRouter<TUser = unknown>(routes: Array<ProtectedRouteObject<TUser>>, options: CreateAccessRouterConfig<TUser>, routerOptions?: CreateAccessRouterOptions): ReturnType<typeof createBrowserRouter>;
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
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={};/**
|
|
2
|
+
* @license React
|
|
3
|
+
* react-jsx-runtime.production.js
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
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 $={};/**
|
|
10
|
+
* @license React
|
|
11
|
+
* react-jsx-runtime.development.js
|
|
12
|
+
*
|
|
13
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
14
|
+
*
|
|
15
|
+
* This source code is licensed under the MIT license found in the
|
|
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:
|
|
18
|
+
let props = %s;
|
|
19
|
+
<%s {...props} />
|
|
20
|
+
React keys must be passed directly to JSX without using spread:
|
|
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;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { AccessRoute, useRouteAccess } from './AccessRoute';
|
|
2
|
+
export { createAccessRouter } from './createAccessRouter';
|
|
3
|
+
export type { AccessRouteProps, CreateAccessRouterConfig, CreateAccessRouterOptions, ProtectedRouteObject, RouterAccessLevel, RouterRouteConfig, } from './types';
|
|
4
|
+
export type { AccessContextValue, AccessProviderProps, NavigationConfig, RouteProtection, } from '@react-protected/react';
|
|
5
|
+
export { AccessProvider, HasAccess, useAccess, useHasAccess, } from '@react-protected/react';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,463 @@
|
|
|
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";
|
|
5
|
+
import { createGuard as le } from "@react-protected/core";
|
|
6
|
+
var M = { exports: {} }, y = {};
|
|
7
|
+
/**
|
|
8
|
+
* @license React
|
|
9
|
+
* react-jsx-runtime.production.js
|
|
10
|
+
*
|
|
11
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
12
|
+
*
|
|
13
|
+
* This source code is licensed under the MIT license found in the
|
|
14
|
+
* LICENSE file in the root directory of this source tree.
|
|
15
|
+
*/
|
|
16
|
+
var Q;
|
|
17
|
+
function fe() {
|
|
18
|
+
if (Q) return y;
|
|
19
|
+
Q = 1;
|
|
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) {
|
|
24
|
+
o = {};
|
|
25
|
+
for (var m in a)
|
|
26
|
+
m !== "key" && (o[m] = a[m]);
|
|
27
|
+
} else o = a;
|
|
28
|
+
return a = o.ref, {
|
|
29
|
+
$$typeof: r,
|
|
30
|
+
type: u,
|
|
31
|
+
key: i,
|
|
32
|
+
ref: a !== void 0 ? a : null,
|
|
33
|
+
props: o
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return y.Fragment = n, y.jsx = s, y.jsxs = s, y;
|
|
37
|
+
}
|
|
38
|
+
var S = {};
|
|
39
|
+
/**
|
|
40
|
+
* @license React
|
|
41
|
+
* react-jsx-runtime.development.js
|
|
42
|
+
*
|
|
43
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
44
|
+
*
|
|
45
|
+
* This source code is licensed under the MIT license found in the
|
|
46
|
+
* LICENSE file in the root directory of this source tree.
|
|
47
|
+
*/
|
|
48
|
+
var K;
|
|
49
|
+
function de() {
|
|
50
|
+
return K || (K = 1, process.env.NODE_ENV !== "production" && function() {
|
|
51
|
+
function r(e) {
|
|
52
|
+
if (e == null) return null;
|
|
53
|
+
if (typeof e == "function")
|
|
54
|
+
return e.$$typeof === oe ? null : e.displayName || e.name || null;
|
|
55
|
+
if (typeof e == "string") return e;
|
|
56
|
+
switch (e) {
|
|
57
|
+
case P:
|
|
58
|
+
return "Fragment";
|
|
59
|
+
case j:
|
|
60
|
+
return "Profiler";
|
|
61
|
+
case A:
|
|
62
|
+
return "StrictMode";
|
|
63
|
+
case x:
|
|
64
|
+
return "Suspense";
|
|
65
|
+
case z:
|
|
66
|
+
return "SuspenseList";
|
|
67
|
+
case ne:
|
|
68
|
+
return "Activity";
|
|
69
|
+
}
|
|
70
|
+
if (typeof e == "object")
|
|
71
|
+
switch (typeof e.tag == "number" && console.error(
|
|
72
|
+
"Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."
|
|
73
|
+
), e.$$typeof) {
|
|
74
|
+
case k:
|
|
75
|
+
return "Portal";
|
|
76
|
+
case C:
|
|
77
|
+
return e.displayName || "Context";
|
|
78
|
+
case N:
|
|
79
|
+
return (e._context.displayName || "Context") + ".Consumer";
|
|
80
|
+
case w:
|
|
81
|
+
var t = e.render;
|
|
82
|
+
return e = e.displayName, e || (e = t.displayName || t.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e;
|
|
83
|
+
case T:
|
|
84
|
+
return t = e.displayName || null, t !== null ? t : r(e.type) || "Memo";
|
|
85
|
+
case O:
|
|
86
|
+
t = e._payload, e = e._init;
|
|
87
|
+
try {
|
|
88
|
+
return r(e(t));
|
|
89
|
+
} catch {
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
function n(e) {
|
|
95
|
+
return "" + e;
|
|
96
|
+
}
|
|
97
|
+
function s(e) {
|
|
98
|
+
try {
|
|
99
|
+
n(e);
|
|
100
|
+
var t = !1;
|
|
101
|
+
} catch {
|
|
102
|
+
t = !0;
|
|
103
|
+
}
|
|
104
|
+
if (t) {
|
|
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(
|
|
108
|
+
t,
|
|
109
|
+
"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",
|
|
110
|
+
l
|
|
111
|
+
), n(e);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function u(e) {
|
|
115
|
+
if (e === P) return "<>";
|
|
116
|
+
if (typeof e == "object" && e !== null && e.$$typeof === O)
|
|
117
|
+
return "<...>";
|
|
118
|
+
try {
|
|
119
|
+
var t = r(e);
|
|
120
|
+
return t ? "<" + t + ">" : "<...>";
|
|
121
|
+
} catch {
|
|
122
|
+
return "<...>";
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function a() {
|
|
126
|
+
var e = F.A;
|
|
127
|
+
return e === null ? null : e.getOwner();
|
|
128
|
+
}
|
|
129
|
+
function o() {
|
|
130
|
+
return Error("react-stack-top-frame");
|
|
131
|
+
}
|
|
132
|
+
function i(e) {
|
|
133
|
+
if (V.call(e, "key")) {
|
|
134
|
+
var t = Object.getOwnPropertyDescriptor(e, "key").get;
|
|
135
|
+
if (t && t.isReactWarning) return !1;
|
|
136
|
+
}
|
|
137
|
+
return e.key !== void 0;
|
|
138
|
+
}
|
|
139
|
+
function m(e, t) {
|
|
140
|
+
function c() {
|
|
141
|
+
q || (q = !0, console.error(
|
|
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
|
+
t
|
|
144
|
+
));
|
|
145
|
+
}
|
|
146
|
+
c.isReactWarning = !0, Object.defineProperty(e, "key", {
|
|
147
|
+
get: c,
|
|
148
|
+
configurable: !0
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
function R() {
|
|
152
|
+
var e = r(this.type);
|
|
153
|
+
return J[e] || (J[e] = !0, console.error(
|
|
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
|
+
)), e = this.props.ref, e !== void 0 ? e : null;
|
|
156
|
+
}
|
|
157
|
+
function p(e, t, c, l, U, D) {
|
|
158
|
+
var f = c.ref;
|
|
159
|
+
return e = {
|
|
160
|
+
$$typeof: _,
|
|
161
|
+
type: e,
|
|
162
|
+
key: t,
|
|
163
|
+
props: c,
|
|
164
|
+
_owner: l
|
|
165
|
+
}, (f !== void 0 ? f : null) !== null ? Object.defineProperty(e, "ref", {
|
|
166
|
+
enumerable: !1,
|
|
167
|
+
get: R
|
|
168
|
+
}) : Object.defineProperty(e, "ref", { enumerable: !1, value: null }), e._store = {}, Object.defineProperty(e._store, "validated", {
|
|
169
|
+
configurable: !1,
|
|
170
|
+
enumerable: !1,
|
|
171
|
+
writable: !0,
|
|
172
|
+
value: 0
|
|
173
|
+
}), Object.defineProperty(e, "_debugInfo", {
|
|
174
|
+
configurable: !1,
|
|
175
|
+
enumerable: !1,
|
|
176
|
+
writable: !0,
|
|
177
|
+
value: null
|
|
178
|
+
}), Object.defineProperty(e, "_debugStack", {
|
|
179
|
+
configurable: !1,
|
|
180
|
+
enumerable: !1,
|
|
181
|
+
writable: !0,
|
|
182
|
+
value: U
|
|
183
|
+
}), Object.defineProperty(e, "_debugTask", {
|
|
184
|
+
configurable: !1,
|
|
185
|
+
enumerable: !1,
|
|
186
|
+
writable: !0,
|
|
187
|
+
value: D
|
|
188
|
+
}), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e;
|
|
189
|
+
}
|
|
190
|
+
function E(e, t, c, l, U, D) {
|
|
191
|
+
var f = t.children;
|
|
192
|
+
if (f !== void 0)
|
|
193
|
+
if (l)
|
|
194
|
+
if (ae(f)) {
|
|
195
|
+
for (l = 0; l < f.length; l++)
|
|
196
|
+
b(f[l]);
|
|
197
|
+
Object.freeze && Object.freeze(f);
|
|
198
|
+
} else
|
|
199
|
+
console.error(
|
|
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
|
+
);
|
|
202
|
+
else b(f);
|
|
203
|
+
if (V.call(t, "key")) {
|
|
204
|
+
f = r(e);
|
|
205
|
+
var g = Object.keys(t).filter(function(se) {
|
|
206
|
+
return se !== "key";
|
|
207
|
+
});
|
|
208
|
+
l = 0 < g.length ? "{key: someKey, " + g.join(": ..., ") + ": ...}" : "{key: someKey}", H[f + l] || (g = 0 < g.length ? "{" + g.join(": ..., ") + ": ...}" : "{}", console.error(
|
|
209
|
+
`A props object containing a "key" prop is being spread into JSX:
|
|
210
|
+
let props = %s;
|
|
211
|
+
<%s {...props} />
|
|
212
|
+
React keys must be passed directly to JSX without using spread:
|
|
213
|
+
let props = %s;
|
|
214
|
+
<%s key={someKey} {...props} />`,
|
|
215
|
+
l,
|
|
216
|
+
f,
|
|
217
|
+
g,
|
|
218
|
+
f
|
|
219
|
+
), H[f + l] = !0);
|
|
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,
|
|
228
|
+
typeof e == "function" ? e.displayName || e.name || "Unknown" : e
|
|
229
|
+
), p(
|
|
230
|
+
e,
|
|
231
|
+
f,
|
|
232
|
+
c,
|
|
233
|
+
a(),
|
|
234
|
+
U,
|
|
235
|
+
D
|
|
236
|
+
);
|
|
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));
|
|
240
|
+
}
|
|
241
|
+
function v(e) {
|
|
242
|
+
return typeof e == "object" && e !== null && e.$$typeof === _;
|
|
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() {
|
|
245
|
+
return null;
|
|
246
|
+
};
|
|
247
|
+
d = {
|
|
248
|
+
react_stack_bottom_frame: function(e) {
|
|
249
|
+
return e();
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
var q, J = {}, B = d.react_stack_bottom_frame.bind(
|
|
253
|
+
d,
|
|
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(
|
|
259
|
+
e,
|
|
260
|
+
t,
|
|
261
|
+
c,
|
|
262
|
+
!1,
|
|
263
|
+
l ? Error("react-stack-top-frame") : B,
|
|
264
|
+
l ? L(u(e)) : X
|
|
265
|
+
);
|
|
266
|
+
}, S.jsxs = function(e, t, c) {
|
|
267
|
+
var l = 1e4 > F.recentlyCreatedOwnerStacks++;
|
|
268
|
+
return E(
|
|
269
|
+
e,
|
|
270
|
+
t,
|
|
271
|
+
c,
|
|
272
|
+
!0,
|
|
273
|
+
l ? Error("react-stack-top-frame") : B,
|
|
274
|
+
l ? L(u(e)) : X
|
|
275
|
+
);
|
|
276
|
+
};
|
|
277
|
+
}()), S;
|
|
278
|
+
}
|
|
279
|
+
process.env.NODE_ENV === "production" ? M.exports = fe() : M.exports = de();
|
|
280
|
+
var h = M.exports;
|
|
281
|
+
function Re(r) {
|
|
282
|
+
const { guard: n } = ee();
|
|
283
|
+
return n.check(r);
|
|
284
|
+
}
|
|
285
|
+
const Pe = ce(({
|
|
286
|
+
access: r,
|
|
287
|
+
roles: n,
|
|
288
|
+
permissions: s,
|
|
289
|
+
meta: u,
|
|
290
|
+
children: a
|
|
291
|
+
}) => {
|
|
292
|
+
const { guard: o, loginPath: i, forbiddenPath: m, defaultPath: R, callbackUrlParam: p } = ee(), E = re();
|
|
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, {});
|
|
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 });
|
|
302
|
+
}
|
|
303
|
+
return /* @__PURE__ */ h.jsx($, { to: m, replace: !0 });
|
|
304
|
+
}
|
|
305
|
+
return a ?? /* @__PURE__ */ h.jsx(Y, {});
|
|
306
|
+
});
|
|
307
|
+
function te(r, n, s, u, a, o) {
|
|
308
|
+
return r === "unauthenticated" ? o ? `${s}?${o}=${encodeURIComponent(n)}` : s : r === "forbidden" ? u : a;
|
|
309
|
+
}
|
|
310
|
+
function me({
|
|
311
|
+
access: r,
|
|
312
|
+
roles: n,
|
|
313
|
+
permissions: s,
|
|
314
|
+
meta: u,
|
|
315
|
+
guard: a,
|
|
316
|
+
routeElement: o,
|
|
317
|
+
RouteComponent: i,
|
|
318
|
+
loginPath: m,
|
|
319
|
+
forbiddenPath: R,
|
|
320
|
+
defaultPath: p,
|
|
321
|
+
callbackUrlParam: E
|
|
322
|
+
}) {
|
|
323
|
+
const b = re(), v = `${b.pathname}${b.search}${b.hash}`;
|
|
324
|
+
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, {});
|
|
327
|
+
}
|
|
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,
|
|
333
|
+
m,
|
|
334
|
+
R,
|
|
335
|
+
p,
|
|
336
|
+
E
|
|
337
|
+
);
|
|
338
|
+
return /* @__PURE__ */ h.jsx($, { to: _, replace: !0 });
|
|
339
|
+
}
|
|
340
|
+
return i ? /* @__PURE__ */ h.jsx(i, {}) : o ?? /* @__PURE__ */ h.jsx(Y, {});
|
|
341
|
+
}
|
|
342
|
+
function W(r, n, s) {
|
|
343
|
+
return /* @__PURE__ */ h.jsx(
|
|
344
|
+
me,
|
|
345
|
+
{
|
|
346
|
+
access: r.protection.access,
|
|
347
|
+
roles: r.protection.roles,
|
|
348
|
+
permissions: r.protection.permissions,
|
|
349
|
+
meta: r.protection.meta,
|
|
350
|
+
guard: r.guard,
|
|
351
|
+
routeElement: n,
|
|
352
|
+
RouteComponent: s ?? null,
|
|
353
|
+
loginPath: r.loginPath,
|
|
354
|
+
forbiddenPath: r.forbiddenPath,
|
|
355
|
+
defaultPath: r.defaultPath,
|
|
356
|
+
callbackUrlParam: r.callbackUrlParam
|
|
357
|
+
}
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
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}`;
|
|
363
|
+
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);
|
|
366
|
+
}
|
|
367
|
+
const o = n.guard.check(n.protection);
|
|
368
|
+
if (!o.allowed) {
|
|
369
|
+
const i = te(
|
|
370
|
+
o.reason,
|
|
371
|
+
a,
|
|
372
|
+
n.loginPath,
|
|
373
|
+
n.forbiddenPath,
|
|
374
|
+
n.defaultPath,
|
|
375
|
+
n.callbackUrlParam
|
|
376
|
+
);
|
|
377
|
+
return Z(i);
|
|
378
|
+
}
|
|
379
|
+
return r(s);
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
async function pe(r) {
|
|
383
|
+
const n = await Promise.all(
|
|
384
|
+
Object.entries(r).map(async ([s, u]) => [s, await (u == null ? void 0 : u())])
|
|
385
|
+
);
|
|
386
|
+
return Object.fromEntries(n);
|
|
387
|
+
}
|
|
388
|
+
function he(r, n) {
|
|
389
|
+
if (!r) return;
|
|
390
|
+
const s = typeof r == "function" ? r : () => pe(r);
|
|
391
|
+
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)
|
|
399
|
+
};
|
|
400
|
+
return n.hasStaticUi || (p.element = W(n, a, o ?? null)), p;
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
function Te(r, n, s) {
|
|
404
|
+
const {
|
|
405
|
+
loginPath: u = "/login",
|
|
406
|
+
forbiddenPath: a = "/403",
|
|
407
|
+
defaultPath: o = "/",
|
|
408
|
+
callbackUrlParam: i,
|
|
409
|
+
...m
|
|
410
|
+
} = n, R = le(m), p = (E) => E.map((b) => {
|
|
411
|
+
const {
|
|
412
|
+
access: v,
|
|
413
|
+
roles: d,
|
|
414
|
+
permissions: _,
|
|
415
|
+
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))
|
|
425
|
+
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
|
|
433
|
+
};
|
|
434
|
+
const T = {
|
|
435
|
+
guard: R,
|
|
436
|
+
protection: { access: v, roles: d, permissions: _, meta: k },
|
|
437
|
+
hasStaticUi: A != null || j != null,
|
|
438
|
+
loginPath: u,
|
|
439
|
+
forbiddenPath: a,
|
|
440
|
+
defaultPath: o,
|
|
441
|
+
callbackUrlParam: i
|
|
442
|
+
}, O = T.hasStaticUi || !w ? W(T, A, j ?? null) : void 0;
|
|
443
|
+
return {
|
|
444
|
+
...x,
|
|
445
|
+
element: O,
|
|
446
|
+
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
|
|
451
|
+
};
|
|
452
|
+
});
|
|
453
|
+
return ie(p(r), s);
|
|
454
|
+
}
|
|
455
|
+
export {
|
|
456
|
+
ke as AccessProvider,
|
|
457
|
+
Pe as AccessRoute,
|
|
458
|
+
je as HasAccess,
|
|
459
|
+
Te as createAccessRouter,
|
|
460
|
+
we as useAccess,
|
|
461
|
+
Oe as useHasAccess,
|
|
462
|
+
Re as useRouteAccess
|
|
463
|
+
};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AccessLevel, GuardOptions } from '@react-protected/core';
|
|
2
|
+
import { NavigationConfig, RouteProtection } from '@react-protected/react';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
import { createBrowserRouter, RouteObject } from 'react-router-dom';
|
|
5
|
+
export type RouterAccessLevel = AccessLevel | 'guest-only';
|
|
6
|
+
export type RouterRouteConfig = Omit<RouteProtection, 'access'> & {
|
|
7
|
+
access?: RouterAccessLevel;
|
|
8
|
+
};
|
|
9
|
+
export type AccessRouteProps = RouterRouteConfig & {
|
|
10
|
+
children?: ReactNode;
|
|
11
|
+
};
|
|
12
|
+
export type ProtectedRouteObject<TUser = unknown> = Omit<RouteObject, 'children'> & RouterRouteConfig & {
|
|
13
|
+
children?: Array<ProtectedRouteObject<TUser>>;
|
|
14
|
+
};
|
|
15
|
+
export type CreateAccessRouterOptions = Parameters<typeof createBrowserRouter>[1];
|
|
16
|
+
export type CreateAccessRouterConfig<TUser = unknown> = GuardOptions<TUser> & NavigationConfig;
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@react-protected/react-router",
|
|
3
|
+
"version": "0.1.1-beta.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "React Router data router adapter for react-protected",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/astakhovaskold/react-protected",
|
|
12
|
+
"directory": "packages/react-router"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"import": "./dist/index.js",
|
|
20
|
+
"require": "./dist/index.cjs",
|
|
21
|
+
"types": "./dist/index.d.ts"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@react-protected/react": "0.1.1-beta.0",
|
|
29
|
+
"@react-protected/core": "0.1.1-beta.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"react": ">=18.0.0",
|
|
33
|
+
"react-dom": ">=18.0.0",
|
|
34
|
+
"react-router-dom": ">=6.4.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@testing-library/react": "^16.3.2",
|
|
38
|
+
"@vitejs/plugin-react": "^4.0.0",
|
|
39
|
+
"jsdom": "^29.1.1",
|
|
40
|
+
"react": "^19.2.0",
|
|
41
|
+
"react-dom": "^19.2.0",
|
|
42
|
+
"react-router-dom": "^7.15.0",
|
|
43
|
+
"vite": "^5.0.0",
|
|
44
|
+
"vite-plugin-dts": "^4.0.0"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "vite build",
|
|
48
|
+
"test": "vitest run"
|
|
49
|
+
}
|
|
50
|
+
}
|