| 1 |
- import{i as e}from"./rolldown-runtime-aKtaBQYM.js";import{a as t,k as n,l as r,m as i,t as a,w as o}from"./client-DSw2TdH4.js";var s=e(n(),1),c=o(),l=(0,s.createContext)(void 0);function u({children:e}){let[n,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),n=/^\/(?:overlay\/[^/]+|camwall)\/?$/i.test(window.location.pathname)?null:e.get(`token`);if(n){i(n,`session`),e.delete(`token`);let t=e.toString(),r=window.location.pathname+(t?`?${t}`:``)+window.location.hash;window.history.replaceState({},``,r)}let s=await t.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 t.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),n&&e===n&&i(n,`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 t.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 t.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,n,r=`session`)=>{let a=await t.login({username:e,password:n});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),t.logout().catch(()=>{}),window.location.href=`/login`},D=async()=>{await v()},O=(0,s.useMemo)(()=>new Set(n?.permissions??[]),[n?.permissions]),k=(0,s.useMemo)(()=>u?n?.is_admin??!1:!0,[u,n?.is_admin]),A=(0,s.useCallback)(e=>!u||k?!0:O.has(e),[u,k,O]),j=(0,s.useCallback)((...e)=>!u||k?!0:e.some(e=>O.has(e)),[u,k,O]),M=(0,s.useCallback)((e,t,r)=>{if(!u||k)return!0;let i=`${e}:${t}_all`,a=`${e}:${t}_own`;return O.has(i)?!0:O.has(a)?r!=null&&r===n?.id:!1},[u,k,O,n?.id]);return(0,c.jsx)(l.Provider,{value:{user:n,authEnabled:u,loading:m,isAdmin:k,login:w,loginWithToken:T,logout:E,refreshAuth:D,hasPermission:A,hasAnyPermission:j,canModify:M},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};
|