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