UNPKG

z0rath-react

Version:

React component wrapper for Z0rath (Centralized Authorization System)

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