import { ZitadelAuth } from "./chunk-NFH7JLJM.js"; // src/adapters/react.tsx import { createContext, useContext, useEffect, useState, useSyncExternalStore } from "react"; import { jsx } from "react/jsx-runtime"; var AuthContext = createContext(null); function ZitadelProvider({ children, ...config }) { const [auth] = useState(() => new ZitadelAuth(config)); const [ready, setReady] = useState(false); useEffect(() => { auth.init().then(() => { setReady(true); if (!auth.isAuthenticated && !auth.error) { auth.login(); } }); }, [auth]); if (!ready) return null; return /* @__PURE__ */ jsx(AuthContext.Provider, { value: auth, children }); } function useAuth() { const auth = useContext(AuthContext); if (!auth) throw new Error("useAuth must be used within a ZitadelProvider"); const state = useSyncExternalStore( (cb) => auth.onAuthChange(cb), () => ({ isAuthenticated: auth.isAuthenticated, isLoading: auth.isLoading, user: auth.user, error: auth.error }) ); return { ...state, login: () => auth.login(), logout: () => auth.logout(), fetch: (url, init) => auth.fetch(url, init) }; } export { ZitadelProvider, useAuth };