(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[2308],{75319:function(e,t,r){Promise.resolve().then(r.bind(r,6151))},6151:function(e,t,r){"use strict";r.r(t),r.d(t,{default:function(){return u}});var a=r(57437),o=r(65043),n=r(66037),s=r(83434),l=r(99376),c=r(2265);let i=(0,n.S)(s.G);function u(){let e=(0,l.useRouter)(),[t,r]=(0,c.useState)(!0);return((0,c.useEffect)(()=>{(async function(){try{let{authenticated:t,redirectTo:a}=await i.check();t?e.push(a||"/"):r(!1)}catch(e){r(!1)}})()},[e]),t)?(0,a.jsx)("div",{className:"flex h-screen w-screen items-center justify-center",children:(0,a.jsx)("div",{className:"h-8 w-8 animate-spin rounded-full border-4 border-primary border-t-transparent"})}):(0,a.jsx)(o.W,{type:"login"})}},83434:function(e,t,r){"use strict";r.d(t,{G:function(){return s}});var a=r(49112);let o="token",n="baasix_access_token",s=(0,a.ql)({url:window.location.origin,authMode:"jwt",autoRefresh:!0,onAuthStateChange:(e,t)=>{console.log("[SDK] Auth state changed:",e,null==t?void 0:t.email)}});{let e=localStorage.getItem(o)||localStorage.getItem(n);e&&(s.setToken(e),localStorage.setItem(o,e),localStorage.setItem(n,e)),window.addEventListener("storage",e=>{(e.key===o||e.key===n)&&(e.newValue?(s.setToken(e.newValue),localStorage.setItem(o,e.newValue),localStorage.setItem(n,e.newValue)):s.setToken(""))})}},66037:function(e,t,r){"use strict";r.d(t,{S:function(){return c}});var a=r(49112);let o="token",n="baasix_access_token",s="user",l="baasix_user",c=e=>({login:async t=>{let{email:r,password:c}=t;try{let t=await e.auth.login({email:r,password:c});if(t.user){var i,u;if((null===(u=t.role)||void 0===u?void 0:null===(i=u.name)||void 0===i?void 0:i.toLowerCase())!=="administrator")return{success:!1,error:{message:"Access denied",name:"Only administrators can access this application"}};t.token&&(localStorage.setItem(o,t.token),localStorage.setItem(n,t.token));let e=JSON.stringify(t.user);return localStorage.setItem(s,e),localStorage.setItem(l,e),{success:!0,redirectTo:"/"}}return{success:!1,error:{message:"Login failed",name:"Invalid email or password"}}}catch(e){return{success:!1,error:{message:"Login failed",name:e instanceof a.NQ?e.message:"Invalid email or password"}}}},logout:async()=>{localStorage.removeItem(o),localStorage.removeItem(n),localStorage.removeItem(s),localStorage.removeItem(l),localStorage.removeItem("baasix_refresh_token"),localStorage.removeItem("baasix_token_expiry");try{await e.auth.logout()}catch(e){console.error("Logout error:",e)}return{success:!0,redirectTo:"/login"}},check:async()=>localStorage.getItem(o)||localStorage.getItem(n)?{authenticated:!0}:{authenticated:!1,error:{message:"Not authenticated",name:"Unauthorized"},redirectTo:"/login"},getPermissions:async()=>{try{let t=localStorage.getItem(s)||localStorage.getItem(l);if(t){var e;let r=JSON.parse(t);return(null==r?void 0:null===(e=r.role)||void 0===e?void 0:e.permissions)||[]}}catch(e){}return[]},getIdentity:async()=>{try{let t=localStorage.getItem(s)||localStorage.getItem(l);if(t)return JSON.parse(t);let r=await e.auth.getUser();if(r){let e=JSON.stringify(r);localStorage.setItem(s,e),localStorage.setItem(l,e)}return r}catch(e){return null}},onError:async e=>{let t=e instanceof a.NQ?e.status:null==e?void 0:e.status;return 401===t||403===t?{logout:!0,redirectTo:"/login",error:{message:"Unauthorized",name:"Unauthorized"}}:{}}})}},function(e){e.O(0,[7999,5094,2676,6869,8473,4043,5101,2971,2117,1744],function(){return e(e.s=75319)}),_N_E=e.O()}]);