z0rath-react
Version:
React component wrapper for Z0rath (Centralized Authorization System)
3 lines (2 loc) • 5.64 kB
JavaScript
import e,{useRef as n,createContext as r,useContext as t,useState as o,useEffect as i,useCallback as c}from"react";import{jsx as u,Fragment as a,jsxs as s}from"react/jsx-runtime";import l from"events";var f=function(){return f=Object.assign||function(e){for(var n,r=1,t=arguments.length;r<t;r++)for(var o in n=arguments[r])Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o]);return e},f.apply(this,arguments)};function v(e,n,r,t){return new(r||(r=Promise))((function(o,i){function c(e){try{a(t.next(e))}catch(e){i(e)}}function u(e){try{a(t.throw(e))}catch(e){i(e)}}function a(e){var n;e.done?o(e.value):(n=e.value,n instanceof r?n:new r((function(e){e(n)}))).then(c,u)}a((t=t.apply(e,n||[])).next())}))}function h(e,n){var r,t,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},c=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return c.next=u(0),c.throw=u(1),c.return=u(2),"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c;function u(u){return function(a){return function(u){if(r)throw new TypeError("Generator is already executing.");for(;c&&(c=0,u[0]&&(i=0)),i;)try{if(r=1,t&&(o=2&u[0]?t.return:u[0]?t.throw||((o=t.return)&&o.call(t),0):t.next)&&!(o=o.call(t,u[1])).done)return o;switch(t=0,o&&(u=[2&u[0],o.value]),u[0]){case 0:case 1:o=u;break;case 4:return i.label++,{value:u[1],done:!1};case 5:i.label++,t=u[1],u=[0];continue;case 7:u=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==u[0]&&2!==u[0])){i=0;continue}if(3===u[0]&&(!o||u[1]>o[0]&&u[1]<o[3])){i.label=u[1];break}if(6===u[0]&&i.label<o[1]){i.label=o[1],o=u;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(u);break}o[2]&&i.ops.pop(),i.trys.pop();continue}u=n.call(e,i)}catch(e){u=[6,e],t=0}finally{r=o=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,a])}}}"function"==typeof SuppressedError&&SuppressedError;var p,d=r({}),b=function(){return t(d)},y=(p=new l,{addListener:function(e,n){p.addListener(e,n)},removeListener:function(e,n){p.removeListener(e,n)},emit:function(e,n){p.emit(e,n)},once:function(e,n){p.once(e,n)},removeAllListeners:function(e){p.removeAllListeners(e)}}),m=function(e){return s("svg",f({height:32,style:{overflow:"visible"},viewBox:"0 0 32 32",width:32,xmlSpace:"preserve",xmlns:"http://www.w3.org/2000/svg"},e,{children:[u("circle",{cx:16,cy:16,r:16,style:{fill:"#d72828"}}),u("path",{d:"M14.5 25h3v-3h-3zm0-19v13h3V6z",style:{fill:"#e6e6e6"}})]}))},w=function(){var e=b(),r=e.apiKey,t=e.user,c=o(null),s=c[0],l=c[1],f=o(null),p=f[0],d=f[1],w=n(null),S=n(null);return i((function(){if(p){var e=new WebSocket("wss://z0rath-api.zonezero.dev/api/v1/websockets/ws?ticket=".concat(p));return w.current=e,e.onopen=function(){console.debug("WebSocket connection established")},e.onmessage=function(e){try{switch(JSON.parse(e.data).action){case"invalidate-permissions":y.emit("INVALIDATE_PERMISSIONS",e);break;case"invalidate-attributes":y.emit("INVALIDATE_ATTRIBUTES",e)}}catch(e){console.error(e)}},e.onerror=function(e){console.error("WebSocket error:",e)},e.onclose=function(){console.debug("WebSocket connection closed")},function(){var e;null===(e=w.current)||void 0===e||e.close()}}}),[p]),i((function(){if(t){return v(void 0,void 0,void 0,(function(){var e,n,o,i;return h(this,(function(c){switch(c.label){case 0:return c.trys.push([0,3,,4]),null===(i=S.current)||void 0===i||i.abort(),e=new AbortController,S.current=e,[4,fetch("https://z0rath-api.zonezero.dev/api/v1/websockets/issue-ticket",{method:"POST",headers:{"x-api-key":r},body:JSON.stringify({user:t}),signal:e.signal})];case 1:return[4,c.sent().json()];case 2:return n=c.sent(),d(n.ticket),l(null),[3,4];case 3:return"AbortError"===(o=c.sent()).name?[2]:(l("Unable to establish connection"),console.error("Error fetching ticket:",o),[3,4]);case 4:return[2]}}))})),function(){var e;null===(e=S.current)||void 0===e||e.abort()}}}),[t,r]),u(a,{children:null!==s?u(m,{style:{position:"absolute",width:20,height:20}}):null})},S=function(e){var r=b(),t=r.user,u=r.apiKey,a=o(),s=a[0],l=a[1],f=n(null),p=c((function(){var n,r;if(t){var o="https://z0rath-api.zonezero.dev/api/v1/authorization/has_permission?user=".concat(t,"&slug=").concat(e);null===(n=f.current)||void 0===n||n.abort();var i=new AbortController;f.current=i,fetch(o,{method:"GET",headers:{"Content-Type":"application/json","x-api-key":u},credentials:"omit",signal:null===(r=f.current)||void 0===r?void 0:r.signal}).then((function(e){return v(void 0,void 0,void 0,(function(){var n;return h(this,(function(r){switch(r.label){case 0:if(e.ok)return[3,1];if(403!==e.status)throw new Error("HTTP error! Status: ".concat(e.status));return l(!1),[3,3];case 1:return[4,e.json()];case 2:n=r.sent(),l(n.hasPermission),r.label=3;case 3:return[2]}}))}))})).catch((function(e){"AbortError"!==e.name&&console.error("Error fetching permission:",e)}))}else l(!1)}),[e,t,u]);i((function(){return p(),function(){var e;null===(e=f.current)||void 0===e||e.abort()}}),[p]);var d,m,w=((m=n(d=p)).current=d,m),S=c((function(){return w.current()}),[]);return i((function(){return p(),y.addListener("INVALIDATE_PERMISSIONS",S),function(){y.removeListener("INVALIDATE_PERMISSIONS",S)}}),[]),s},g=function(e){var r=e.slug,t=e.noPermissionCallback,o=e.children,i=S(r),c=!1,u=n(i);return u.current&&!i&&(c=!0),u.current=i,i?o:(null==t||t(c),null)},k=function(n){var r=n.children,t=n.apiKey,o=n.user,i=e.useState(null!=o?o:null),c=i[0],a=i[1];return s(d.Provider,{value:{user:c,apiKey:t,setUser:a},children:[u(w,{}),r]})};export{g as HasPermission,k as Z0rathProvider,b as useZ0rath};
//# sourceMappingURL=index.js.map