| 1 |
- import{i as e}from"./chunk-aKtaBQYM.js";import{O as t,a as n,l as r,m as i,t as a,w as o}from"./client-p9vNUtaN.js";var s=e(t(),1),c=o(),l=(0,s.createContext)(void 0);function u({children:e}){let[t,o]=(0,s.useState)(null),[u,d]=(0,s.useState)(!1),[f,p]=(0,s.useState)(!1),[m,h]=(0,s.useState)(!0),g=(0,s.useRef)(!1),_=(0,s.useRef)(!0),v=async()=>{try{let e=new URLSearchParams(window.location.search),t=/^\/(?:overlay\/[^/]+|camwall)\/?$/i.test(window.location.pathname)?null:e.get(`token`);if(t){i(t,`session`),e.delete(`token`);let n=e.toString(),r=window.location.pathname+(n?`?${n}`:``)+window.location.hash;window.history.replaceState({},``,r)}let s=await n.getAuthStatus();if(!_.current)return;if(d(s.auth_enabled),p(s.requires_setup),s.auth_enabled){let e=r();if(e){let r=null,s=!1;for(let e=1;e<=3;e++)try{r=await n.getCurrentUser();break}catch(t){if(!_.current)return;if(t instanceof a&&t.status===401){s=!0;break}e<3&&await new Promise(t=>setTimeout(t,400*e))}if(!_.current)return;r?(o(r),t&&e===t&&i(t,`persistent`)):(s&&i(null),o(null))}else o(null)}else o(null)}catch{if(!_.current)return;d(!1),o(null)}finally{_.current&&h(!1)}},y=async()=>{let e;try{e=await n.getAuthStatus()}catch{return!1}if(!_.current)return!0;if(d(e.auth_enabled),p(e.requires_setup),!e.auth_enabled||!r())return o(null),!0;try{let e=await n.getCurrentUser();return _.current&&o(e),!0}catch(e){return!(e instanceof a)||e.status!==401&&e.status!==403?!1:(_.current&&(e.status===401&&i(null),o(null)),!0)}},b=(0,s.useRef)(null),x=(0,s.useRef)(0),S=async()=>{b.current!==null&&(clearTimeout(b.current),b.current=null);let e=await y();if(!_.current)return;if(e){x.current=0;return}let t=x.current?Math.min(x.current*2,6e4):2e3;x.current=t,b.current=window.setTimeout(()=>{b.current=null,S()},t)},C=(0,s.useRef)(S);C.current=S,(0,s.useEffect)(()=>{_.current=!0,v();let e=()=>{_.current&&o(null)};window.addEventListener(`auth:expired`,e);let t=()=>{_.current&&C.current()};return window.addEventListener(`auth:recheck`,t),()=>{_.current=!1,window.removeEventListener(`auth:expired`,e),window.removeEventListener(`auth:recheck`,t),b.current!==null&&clearTimeout(b.current)}},[]),(0,s.useEffect)(()=>{if(!m&&f&&!u){let e=window.location.pathname;e!==`/setup`&&!e.startsWith(`/camera/`)&&!g.current&&(g.current=!0,window.location.href=`/setup`)}else f||(g.current=!1)},[m,f,u]);let w=async(e,t,r=`session`)=>{let a=await n.login({username:e,password:t});return!a.requires_2fa&&a.access_token&&(i(a.access_token,r),await v()),a},T=(e,t,n=`session`)=>{i(e,n),o(t),d(!0)},E=()=>{i(null),o(null),n.logout().catch(()=>{}),window.location.href=`/login`},D=async()=>{if(u&&r())try{let e=await n.getCurrentUser();_.current&&o(e)}catch{i(null),_.current&&o(null)}},O=async()=>{await v()},k=(0,s.useMemo)(()=>new Set(t?.permissions??[]),[t?.permissions]),A=(0,s.useMemo)(()=>u?t?.is_admin??!1:!0,[u,t?.is_admin]),j=(0,s.useCallback)(e=>!u||A?!0:k.has(e),[u,A,k]),M=(0,s.useCallback)((...e)=>!u||A?!0:e.some(e=>k.has(e)),[u,A,k]),N=(0,s.useCallback)((...e)=>!u||A?!0:e.every(e=>k.has(e)),[u,A,k]),P=(0,s.useCallback)((e,n,r)=>{if(!u||A)return!0;let i=`${e}:${n}_all`,a=`${e}:${n}_own`;return k.has(i)?!0:k.has(a)?r==null?!1:r===t?.id:!1},[u,A,k,t?.id]);return(0,c.jsx)(l.Provider,{value:{user:t,authEnabled:u,requiresSetup:f,loading:m,isAdmin:A,login:w,loginWithToken:T,logout:E,refreshUser:D,refreshAuth:O,hasPermission:j,hasAnyPermission:M,hasAllPermissions:N,canModify:P},children:e})}function d(){let e=(0,s.useContext)(l);if(e===void 0)throw Error(`useAuth must be used within an AuthProvider`);return e}export{d as n,u as t};
|