(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[2308],{75319:function(e,t,n){Promise.resolve().then(n.bind(n,6151))},99376:function(e,t,n){"use strict";var r=n(35475);n.o(r,"usePathname")&&n.d(t,{usePathname:function(){return r.usePathname}}),n.o(r,"useRouter")&&n.d(t,{useRouter:function(){return r.useRouter}}),n.o(r,"useSearchParams")&&n.d(t,{useSearchParams:function(){return r.useSearchParams}})},6151:function(e,t,n){"use strict";n.r(t),n.d(t,{default:function(){return c}});var r=n(57437),o=n(65043),a=n(66037),s=n(83434),i=n(99376),u=n(2265);let l=(0,a.S)(s.G);function c(){let e=(0,i.useRouter)(),[t,n]=(0,u.useState)(!0);return((0,u.useEffect)(()=>{(async function(){try{let{authenticated:t,redirectTo:r}=await l.check();t?e.push(r||"/"):n(!1)}catch(e){n(!1)}})()},[e]),t)?(0,r.jsx)("div",{className:"flex h-screen w-screen items-center justify-center",children:(0,r.jsx)("div",{className:"h-8 w-8 animate-spin rounded-full border-4 border-primary border-t-transparent"})}):(0,r.jsx)(o.W,{type:"login"})}},16865:function(e,t,n){"use strict";n.d(t,{D3:function(){return y},KF:function(){return h},Vj:function(){return f},ez:function(){return c},k3:function(){return S},pw:function(){return d},vF:function(){return m}});let r="token",o="user",a="baasix_access_token",s="baasix_user",i="baasix_role",u="baasix_permissions",l="baasix_tenant_info";function c(e){if(e.token&&(localStorage.setItem(r,e.token),localStorage.setItem(a,e.token)),e.user){let t=JSON.stringify(e.user);localStorage.setItem(o,t),localStorage.setItem(s,t)}e.role&&localStorage.setItem(i,JSON.stringify(e.role)),e.permissions&&localStorage.setItem(u,JSON.stringify(e.permissions)),e.tenant&&localStorage.setItem(l,JSON.stringify(e.tenant))}function f(){for(let e of[r,o,a,s,i,u,l,"baasix_refresh_token","baasix_token_expiry","baasix_tenant"])localStorage.removeItem(e)}function g(e){try{let t=localStorage.getItem(e);return t?JSON.parse(t):null}catch(t){return console.warn('Failed to parse localStorage key "'.concat(e,'"')),null}}function m(){var e;let t=null!==(e=g(o))&&void 0!==e?e:g(s);return t&&"object"==typeof t&&"string"==typeof t.id&&t.id?t:null}function d(){return g(i)}function h(){return g(l)}function S(){var e;return null!==(e=g(u))&&void 0!==e?e:[]}function y(){var e;let t=d();return!t||(null===(e=t.name)||void 0===e?void 0:e.toLowerCase())==="administrator"}},66037:function(e,t,n){"use strict";n.d(t,{S:function(){return i}});var r=n(6488),o=n(16865);let a="user",s="baasix_user",i=e=>({login:async t=>{let{email:n,password:a}=t;try{let t=await e.auth.login({email:n,password:a});if(t.user){var s,i,u;return null==t.role&&console.warn("Login response missing role — UI role gating will treat this session as legacy/admin"),(0,o.ez)({token:t.token,user:t.user,role:null!==(s=t.role)&&void 0!==s?s:null,permissions:null!==(i=t.permissions)&&void 0!==i?i:null,tenant:null!==(u=t.tenant)&&void 0!==u?u:null}),window.location.href="".concat("/admin","/"),{success:!0}}return{success:!1,error:{message:"Login failed",name:"Invalid email or password"}}}catch(e){return{success:!1,error:{message:"Login failed",name:e instanceof r.NQ?e.message:"Invalid email or password"}}}},logout:async()=>{(0,o.Vj)();try{await e.auth.logout()}catch(e){console.error("Logout error:",e)}return{success:!0,redirectTo:"/login"}},check:async()=>localStorage.getItem("token")||localStorage.getItem("baasix_access_token")?{authenticated:!0}:{authenticated:!1,error:{message:"Not authenticated",name:"Unauthorized"},redirectTo:"/login"},getPermissions:async()=>{let e=(0,o.k3)();if(e.length>0)return e;try{let e=localStorage.getItem(a)||localStorage.getItem(s);if(e){var t;let n=JSON.parse(e);return(null==n?void 0:null===(t=n.role)||void 0===t?void 0:t.permissions)||[]}}catch(e){}return[]},getIdentity:async()=>{try{let t=localStorage.getItem(a)||localStorage.getItem(s);if(t)return JSON.parse(t);let n=await e.auth.getUser();if(n){let e=JSON.stringify(n);localStorage.setItem(a,e),localStorage.setItem(s,e)}return n}catch(e){return null}},onError:async e=>401===(e instanceof r.NQ?e.status:null==e?void 0:e.status)?{logout:!0,redirectTo:"/login",error:{message:"Unauthorized",name:"Unauthorized"}}:{error:e}})}},function(e){e.O(0,[7999,6137,8042,9501,8473,6774,3490,5101,2971,2117,1744],function(){return e(e.s=75319)}),_N_E=e.O()}]);