react-easy-router
Advanced tools
Comparing version 2.1.2 to 2.2.0
@@ -17,2 +17,8 @@ # CHANGELOG | ||
## v2.2.0 | ||
#### Features | ||
- Added support to check auth on multiple roles | ||
## v2.1.2 | ||
@@ -19,0 +25,0 @@ |
@@ -9,9 +9,11 @@ /** | ||
* @param failureRedirect {string} - The path to redirect if the user is not authenticated | ||
* @param roles | ||
* @param children {ReactNode} - The component to render if the user is authenticated | ||
* @returns {ReactElement} | ||
*/ | ||
declare function Protected({ failureRedirect, children, }: { | ||
declare function Protected({ failureRedirect, roles, children, }: { | ||
failureRedirect?: string; | ||
roles?: string[]; | ||
children: ReactNode; | ||
}): ReactElement; | ||
export default Protected; |
@@ -1,2 +0,2 @@ | ||
import e,{useState as t,createContext as n,useContext as r,useEffect as a}from"react";import{Navigate as l,Route as o,Routes as i}from"react-router-dom";function c(){return c=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},c.apply(this,arguments)}const u=/*#__PURE__*/n(null),h=/*#__PURE__*/n(null);function s(){return r(h)}function d({isAuthenticated:n,showLoader:r=!0,loader:a="https://i.imgur.com/FhvNntt.gif",children:l}){const[o,i]=t(null),[c]=t({isAuthenticated:n,showLoader:r,loader:a});/*#__PURE__*/return e.createElement(u.Provider,{value:{auth:o,setAuth:i}},/*#__PURE__*/e.createElement(h.Provider,{value:{config:c}},l))}function m(){const{config:t}=s();/*#__PURE__*/return e.createElement("div",{className:"loader",style:{height:"100vh",width:"100%",backgroundColor:"white",position:"absolute",zIndex:1e3,display:"flex",justifyContent:"center",alignItems:"center"}},/*#__PURE__*/e.createElement("img",{src:t.loader,alt:"loader"}))}function p({failureRedirect:t="/",children:n}){const{auth:o,setAuth:i}=r(u),{config:{isAuthenticated:c,showLoader:h}}=s();return a(()=>{c?c().then(e=>{i(e)}).catch(()=>i(!1)):i(!1)},[c,i]),null===o?h&&/*#__PURE__*/e.createElement(m,null):o?n:/*#__PURE__*/e.createElement(l,{to:t})}function f(t){/*#__PURE__*/return e.createElement(o,t.navigate?{key:t.path,path:t.path,element:/*#__PURE__*/e.createElement(l,{to:t.navigate})}:t.protected?{key:t.path,path:t.path,element:/*#__PURE__*/e.createElement(p,{failureRedirect:t.failureRedirect},t.element)}:{key:t.path,path:t.path,element:t.element})}function g(e,t=""){const n=e.map(e=>{e.path=function(e,t){const n=e.split("/"),r=t.split("/"),a=[...new Set([...n,...r])].join("/");return"/"===a[0]?a:`/${a}`}(t,e.path);let n=[];return e.children&&(n=g(e.children,e.path)),[...n,f(e)]});return n.flat()}function v(t){/*#__PURE__*/return e.createElement(d,c({},t),/*#__PURE__*/e.createElement(i,null,g(t.routes)))}export{v as default}; | ||
import e,{useState as t,createContext as n,useContext as r,useEffect as l}from"react";import{Navigate as a,Route as o,Routes as i}from"react-router-dom";function c(){return c=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},c.apply(this,arguments)}const u=/*#__PURE__*/n(null),h=/*#__PURE__*/n(null);function s(){return r(h)}function d({isAuthenticated:n,showLoader:r=!0,loader:l="https://i.imgur.com/FhvNntt.gif",children:a}){const[o,i]=t(null),[c]=t({isAuthenticated:n,showLoader:r,loader:l});/*#__PURE__*/return e.createElement(u.Provider,{value:{auth:o,setAuth:i}},/*#__PURE__*/e.createElement(h.Provider,{value:{config:c}},a))}function f(){const{config:t}=s();/*#__PURE__*/return e.createElement("div",{className:"loader",style:{height:"100vh",width:"100%",backgroundColor:"white",position:"absolute",zIndex:1e3,display:"flex",justifyContent:"center",alignItems:"center"}},/*#__PURE__*/e.createElement("img",{src:t.loader,alt:"loader"}))}function m({failureRedirect:t="/",roles:n,children:o}){const{auth:i,setAuth:c}=r(u),{config:{isAuthenticated:h,showLoader:d}}=s();return l(()=>{h?h().then(e=>{if(e){if(null!=n&&n.length&&n.includes(e.role))return c(!0);c(null==n||!n.length)}else c(!1)}).catch(()=>c(!1)):c(!1)},[h,c]),null===i?d&&/*#__PURE__*/e.createElement(f,null):i?o:/*#__PURE__*/e.createElement(a,{to:t})}function p(t){/*#__PURE__*/return e.createElement(o,t.navigate?{key:t.path,path:t.path,element:/*#__PURE__*/e.createElement(a,{to:t.navigate})}:t.protected?{key:t.path,path:t.path,element:/*#__PURE__*/e.createElement(m,{failureRedirect:t.failureRedirect,roles:t.roles},t.element)}:{key:t.path,path:t.path,element:t.element})}function g(e,t=""){const n=e.map(e=>{e.path=function(e,t){const n=e.split("/"),r=t.split("/"),l=[...new Set([...n,...r])].join("/");return"/"===l[0]?l:`/${l}`}(t,e.path);let n=[];return e.children&&(n=g(e.children,e.path)),[...n,p(e)]});return n.flat()}function v(t){/*#__PURE__*/return e.createElement(d,c({},t),/*#__PURE__*/e.createElement(i,null,g(t.routes)))}export{v as default}; | ||
//# sourceMappingURL=index.modern.js.map |
@@ -1,2 +0,2 @@ | ||
import e,{useState as t,createContext as n,useContext as r,useEffect as a}from"react";import{Navigate as i,Route as o,Routes as c}from"react-router-dom";function l(){return l=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},l.apply(this,arguments)}var u=/*#__PURE__*/n(null),h=/*#__PURE__*/n(null);function d(){return r(h)}function f(n){var r=n.isAuthenticated,a=n.showLoader,i=void 0===a||a,o=n.loader,c=void 0===o?"https://i.imgur.com/FhvNntt.gif":o,l=n.children,d=t(null),f=d[0],s=d[1],m=t({isAuthenticated:r,showLoader:i,loader:c});/*#__PURE__*/return e.createElement(u.Provider,{value:{auth:f,setAuth:s}},/*#__PURE__*/e.createElement(h.Provider,{value:{config:m[0]}},l))}function s(){var t=d();/*#__PURE__*/return e.createElement("div",{className:"loader",style:{height:"100vh",width:"100%",backgroundColor:"white",position:"absolute",zIndex:1e3,display:"flex",justifyContent:"center",alignItems:"center"}},/*#__PURE__*/e.createElement("img",{src:t.config.loader,alt:"loader"}))}function m(t){var n=t.failureRedirect,o=void 0===n?"/":n,c=t.children,l=r(u),h=l.auth,f=l.setAuth,m=d().config,p=m.isAuthenticated,v=m.showLoader;return a(function(){p?p().then(function(e){f(e)}).catch(function(){return f(!1)}):f(!1)},[p,f]),null===h?v&&/*#__PURE__*/e.createElement(s,null):h?c:/*#__PURE__*/e.createElement(i,{to:o})}function p(t){/*#__PURE__*/return e.createElement(o,t.navigate?{key:t.path,path:t.path,element:/*#__PURE__*/e.createElement(i,{to:t.navigate})}:t.protected?{key:t.path,path:t.path,element:/*#__PURE__*/e.createElement(m,{failureRedirect:t.failureRedirect},t.element)}:{key:t.path,path:t.path,element:t.element})}function v(e,t){void 0===t&&(t="");var n=e.map(function(e){e.path=function(e,t){var n=e.split("/"),r=t.split("/"),a=[].concat(new Set([].concat(n,r))).join("/");return"/"===a[0]?a:"/"+a}(t,e.path);var n=[];return e.children&&(n=v(e.children,e.path)),[].concat(n,[p(e)])});return n.flat()}function g(t){/*#__PURE__*/return e.createElement(f,l({},t),/*#__PURE__*/e.createElement(c,null,v(t.routes)))}export{g as default}; | ||
import e,{useState as t,createContext as n,useContext as r,useEffect as a}from"react";import{Navigate as l,Route as i,Routes as o}from"react-router-dom";function c(){return c=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},c.apply(this,arguments)}var u=/*#__PURE__*/n(null),h=/*#__PURE__*/n(null);function d(){return r(h)}function s(n){var r=n.isAuthenticated,a=n.showLoader,l=void 0===a||a,i=n.loader,o=void 0===i?"https://i.imgur.com/FhvNntt.gif":i,c=n.children,d=t(null),s=d[0],f=d[1],m=t({isAuthenticated:r,showLoader:l,loader:o});/*#__PURE__*/return e.createElement(u.Provider,{value:{auth:s,setAuth:f}},/*#__PURE__*/e.createElement(h.Provider,{value:{config:m[0]}},c))}function f(){var t=d();/*#__PURE__*/return e.createElement("div",{className:"loader",style:{height:"100vh",width:"100%",backgroundColor:"white",position:"absolute",zIndex:1e3,display:"flex",justifyContent:"center",alignItems:"center"}},/*#__PURE__*/e.createElement("img",{src:t.config.loader,alt:"loader"}))}function m(t){var n=t.failureRedirect,i=void 0===n?"/":n,o=t.roles,c=t.children,h=r(u),s=h.auth,m=h.setAuth,p=d().config,v=p.isAuthenticated,g=p.showLoader;return a(function(){v?v().then(function(e){if(e){if(null!=o&&o.length&&o.includes(e.role))return m(!0);m(null==o||!o.length)}else m(!1)}).catch(function(){return m(!1)}):m(!1)},[v,m]),null===s?g&&/*#__PURE__*/e.createElement(f,null):s?c:/*#__PURE__*/e.createElement(l,{to:i})}function p(t){/*#__PURE__*/return e.createElement(i,t.navigate?{key:t.path,path:t.path,element:/*#__PURE__*/e.createElement(l,{to:t.navigate})}:t.protected?{key:t.path,path:t.path,element:/*#__PURE__*/e.createElement(m,{failureRedirect:t.failureRedirect,roles:t.roles},t.element)}:{key:t.path,path:t.path,element:t.element})}function v(e,t){void 0===t&&(t="");var n=e.map(function(e){e.path=function(e,t){var n=e.split("/"),r=t.split("/"),a=[].concat(new Set([].concat(n,r))).join("/");return"/"===a[0]?a:"/"+a}(t,e.path);var n=[];return e.children&&(n=v(e.children,e.path)),[].concat(n,[p(e)])});return n.flat()}function g(t){/*#__PURE__*/return e.createElement(s,c({},t),/*#__PURE__*/e.createElement(o,null,v(t.routes)))}export{g as default}; | ||
//# sourceMappingURL=index.module.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react"),require("react-router-dom")):"function"==typeof define&&define.amd?define(["react","react-router-dom"],t):(e||self).reactEasyRouter=t(e.react,e.reactRouterDom)}(this,function(e,t){function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=/*#__PURE__*/n(e);function r(){return r=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var a in n)Object.prototype.hasOwnProperty.call(n,a)&&(e[a]=n[a])}return e},r.apply(this,arguments)}var o=/*#__PURE__*/e.createContext(null),u=/*#__PURE__*/e.createContext(null);function i(){return e.useContext(u)}function l(t){var n=t.isAuthenticated,r=t.showLoader,i=void 0===r||r,l=t.loader,c=void 0===l?"https://i.imgur.com/FhvNntt.gif":l,d=t.children,f=e.useState(null),h=f[0],s=f[1],p=e.useState({isAuthenticated:n,showLoader:i,loader:c});/*#__PURE__*/return a.default.createElement(o.Provider,{value:{auth:h,setAuth:s}},/*#__PURE__*/a.default.createElement(u.Provider,{value:{config:p[0]}},d))}function c(){var e=i();/*#__PURE__*/return a.default.createElement("div",{className:"loader",style:{height:"100vh",width:"100%",backgroundColor:"white",position:"absolute",zIndex:1e3,display:"flex",justifyContent:"center",alignItems:"center"}},/*#__PURE__*/a.default.createElement("img",{src:e.config.loader,alt:"loader"}))}function d(n){var r=n.failureRedirect,u=void 0===r?"/":r,l=n.children,d=e.useContext(o),f=d.auth,h=d.setAuth,s=i().config,p=s.isAuthenticated,m=s.showLoader;return e.useEffect(function(){p?p().then(function(e){h(e)}).catch(function(){return h(!1)}):h(!1)},[p,h]),null===f?m&&/*#__PURE__*/a.default.createElement(c,null):f?l:/*#__PURE__*/a.default.createElement(t.Navigate,{to:u})}function f(e){/*#__PURE__*/return a.default.createElement(t.Route,e.navigate?{key:e.path,path:e.path,element:/*#__PURE__*/a.default.createElement(t.Navigate,{to:e.navigate})}:e.protected?{key:e.path,path:e.path,element:/*#__PURE__*/a.default.createElement(d,{failureRedirect:e.failureRedirect},e.element)}:{key:e.path,path:e.path,element:e.element})}function h(e,t){void 0===t&&(t="");var n=e.map(function(e){e.path=function(e,t){var n=e.split("/"),a=t.split("/"),r=[].concat(new Set([].concat(n,a))).join("/");return"/"===r[0]?r:"/"+r}(t,e.path);var n=[];return e.children&&(n=h(e.children,e.path)),[].concat(n,[f(e)])});return n.flat()}return function(e){/*#__PURE__*/return a.default.createElement(l,r({},e),/*#__PURE__*/a.default.createElement(t.Routes,null,h(e.routes)))}}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react"),require("react-router-dom")):"function"==typeof define&&define.amd?define(["react","react-router-dom"],t):(e||self).reactEasyRouter=t(e.react,e.reactRouterDom)}(this,function(e,t){function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=/*#__PURE__*/n(e);function a(){return a=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},a.apply(this,arguments)}var l=/*#__PURE__*/e.createContext(null),o=/*#__PURE__*/e.createContext(null);function u(){return e.useContext(o)}function i(t){var n=t.isAuthenticated,a=t.showLoader,u=void 0===a||a,i=t.loader,c=void 0===i?"https://i.imgur.com/FhvNntt.gif":i,f=t.children,d=e.useState(null),s=d[0],h=d[1],p=e.useState({isAuthenticated:n,showLoader:u,loader:c});/*#__PURE__*/return r.default.createElement(l.Provider,{value:{auth:s,setAuth:h}},/*#__PURE__*/r.default.createElement(o.Provider,{value:{config:p[0]}},f))}function c(){var e=u();/*#__PURE__*/return r.default.createElement("div",{className:"loader",style:{height:"100vh",width:"100%",backgroundColor:"white",position:"absolute",zIndex:1e3,display:"flex",justifyContent:"center",alignItems:"center"}},/*#__PURE__*/r.default.createElement("img",{src:e.config.loader,alt:"loader"}))}function f(n){var a=n.failureRedirect,o=void 0===a?"/":a,i=n.roles,f=n.children,d=e.useContext(l),s=d.auth,h=d.setAuth,p=u().config,m=p.isAuthenticated,v=p.showLoader;return e.useEffect(function(){m?m().then(function(e){if(e){if(null!=i&&i.length&&i.includes(e.role))return h(!0);h(null==i||!i.length)}else h(!1)}).catch(function(){return h(!1)}):h(!1)},[m,h]),null===s?v&&/*#__PURE__*/r.default.createElement(c,null):s?f:/*#__PURE__*/r.default.createElement(t.Navigate,{to:o})}function d(e){/*#__PURE__*/return r.default.createElement(t.Route,e.navigate?{key:e.path,path:e.path,element:/*#__PURE__*/r.default.createElement(t.Navigate,{to:e.navigate})}:e.protected?{key:e.path,path:e.path,element:/*#__PURE__*/r.default.createElement(f,{failureRedirect:e.failureRedirect,roles:e.roles},e.element)}:{key:e.path,path:e.path,element:e.element})}function s(e,t){void 0===t&&(t="");var n=e.map(function(e){e.path=function(e,t){var n=e.split("/"),r=t.split("/"),a=[].concat(new Set([].concat(n,r))).join("/");return"/"===a[0]?a:"/"+a}(t,e.path);var n=[];return e.children&&(n=s(e.children,e.path)),[].concat(n,[d(e)])});return n.flat()}return function(e){/*#__PURE__*/return r.default.createElement(i,a({},e),/*#__PURE__*/r.default.createElement(t.Routes,null,s(e.routes)))}}); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -5,2 +5,3 @@ import { ReactElement } from 'react'; | ||
protected?: boolean; | ||
roles?: string[]; | ||
failureRedirect?: string; | ||
@@ -7,0 +8,0 @@ children?: RouteProps[]; |
{ | ||
"name": "react-easy-router", | ||
"version": "2.1.2", | ||
"version": "2.2.0", | ||
"description": "The simplest way to add routing to your React app", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -79,2 +79,3 @@ > We are the first generation to feel the sting of climate change, and we are the last generation that can do something about it. Save our planet 🪴 | ||
protected: true, | ||
roles: ["admin", 'manager'], // Optional: Role specific screen | ||
failureRedirect: '/admin/login', // Optional: Default is '/' | ||
@@ -128,3 +129,3 @@ } | ||
if (token) { | ||
return true; | ||
return { success: true, role: 'admin' }; | ||
} else { | ||
@@ -131,0 +132,0 @@ return false; |
@@ -6,2 +6,3 @@ import { ReactElement } from 'react'; | ||
protected?: boolean; | ||
roles?: string[]; | ||
failureRedirect?: string; | ||
@@ -8,0 +9,0 @@ children?: RouteProps[]; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
77642
475
178