UNPKG

kitcn

Version:

kitcn - React Query integration and CLI tools for Convex

156 lines (155 loc) 7.45 kB
'use client'; import { ConvexProviderWithAuth } from "convex/react"; import * as react from "react"; import * as jotai_x0 from "jotai-x"; import * as react_jsx_runtime0 from "react/jsx-runtime"; import * as jotai_vanilla0 from "jotai/vanilla"; //#region src/react/auth-store.d.ts type FetchAccessTokenFn = (args: { forceRefreshToken: boolean; }) => Promise<string | null>; declare const FetchAccessTokenContext: react.Context<FetchAccessTokenFn | null>; /** Get fetchAccessToken from context (available immediately, no race condition) */ declare const useFetchAccessToken: () => FetchAccessTokenFn | null; type ConvexAuthResult = { isAuthenticated: boolean; isLoading: boolean; }; /** Get auth from bridge context (null if no bridge configured) */ declare const useConvexAuthBridge: () => ConvexAuthResult | null; type AuthStoreState = { /** Callback when mutation/action called while unauthorized. Throws by default. */onMutationUnauthorized: () => void; /** Callback when query called while unauthorized. Noop by default. */ onQueryUnauthorized: (info: { queryName: string; }) => void; /** Custom function to detect UNAUTHORIZED errors. Default checks code or "auth" in message. */ isUnauthorized: (error: unknown) => boolean; /** Cached Convex JWT for HTTP requests */ token: string | null; /** JWT expiration timestamp (ms) */ expiresAt: number | null; /** Auth loading state (synced from useConvexAuth for class methods) */ isLoading: boolean; /** Auth state (synced from useConvexAuth for class methods) */ isAuthenticated: boolean; /** Grace window for freshly seeded auth tokens while session sync catches up */ sessionSyncGraceUntil: number | null; }; declare const AUTH_SESSION_SYNC_GRACE_MS = 10000; declare const isSessionSyncGraceActive: (sessionSyncGraceUntil: number | null) => boolean; /** Decode JWT expiration (ms timestamp) from token */ declare function decodeJwtExp(token: string): number | null; declare const AuthProvider: react.FC<jotai_x0.ProviderProps<{ onMutationUnauthorized: () => void; onQueryUnauthorized: (info: { queryName: string; }) => void; isUnauthorized: (error: unknown) => boolean; token: string | null; expiresAt: number | null; isLoading: boolean; isAuthenticated: boolean; sessionSyncGraceUntil: number | null; }>>, useAuthStore: jotai_x0.UseStoreApi<AuthStoreState, object>, useAuthState: <K extends keyof AuthStoreState>(key: K, options?: string | jotai_x0.UseAtomOptions) => ({ onMutationUnauthorized: jotai_x0.SimpleWritableAtom<() => void>; onQueryUnauthorized: jotai_x0.SimpleWritableAtom<(info: { queryName: string; }) => void>; isUnauthorized: jotai_x0.SimpleWritableAtom<(error: unknown) => boolean>; token: jotai_x0.SimpleWritableAtom<string | null>; expiresAt: jotai_x0.SimpleWritableAtom<number | null>; isLoading: jotai_x0.SimpleWritableAtom<boolean>; isAuthenticated: jotai_x0.SimpleWritableAtom<boolean>; sessionSyncGraceUntil: jotai_x0.SimpleWritableAtom<number | null>; } & object)[K] extends jotai_vanilla0.WritableAtom<infer V, infer A extends unknown[], infer R> ? [V, (...args: A) => R] : never, useAuthValue: <K extends keyof AuthStoreState, S = (({ onMutationUnauthorized: jotai_x0.SimpleWritableAtom<() => void>; onQueryUnauthorized: jotai_x0.SimpleWritableAtom<(info: { queryName: string; }) => void>; isUnauthorized: jotai_x0.SimpleWritableAtom<(error: unknown) => boolean>; token: jotai_x0.SimpleWritableAtom<string | null>; expiresAt: jotai_x0.SimpleWritableAtom<number | null>; isLoading: jotai_x0.SimpleWritableAtom<boolean>; isAuthenticated: jotai_x0.SimpleWritableAtom<boolean>; sessionSyncGraceUntil: jotai_x0.SimpleWritableAtom<number | null>; } & object)[K] extends jotai_vanilla0.Atom<infer V> ? V : never)>(key: K, options?: ({ selector?: ((v: ({ onMutationUnauthorized: jotai_x0.SimpleWritableAtom<() => void>; onQueryUnauthorized: jotai_x0.SimpleWritableAtom<(info: { queryName: string; }) => void>; isUnauthorized: jotai_x0.SimpleWritableAtom<(error: unknown) => boolean>; token: jotai_x0.SimpleWritableAtom<string | null>; expiresAt: jotai_x0.SimpleWritableAtom<number | null>; isLoading: jotai_x0.SimpleWritableAtom<boolean>; isAuthenticated: jotai_x0.SimpleWritableAtom<boolean>; sessionSyncGraceUntil: jotai_x0.SimpleWritableAtom<number | null>; } & object)[K] extends jotai_vanilla0.Atom<infer V_1> ? V_1 : never, prevSelectorOutput?: S | undefined) => S) | undefined; equalityFn?: ((prev: S, next: S) => boolean) | undefined; } & jotai_x0.UseAtomOptions) | undefined, deps?: unknown[]) => S; type AuthStore = ReturnType<typeof useAuthStore>; /** * Safe wrapper around useConvexAuth that doesn't throw when used outside auth provider. * Returns { isAuthenticated: false, isLoading: false } when no auth provider. * * Supports both: * - better-auth users (via AuthProvider) * - @convex-dev/auth users (via ConvexAuthBridge) */ declare function useSafeConvexAuth(): ConvexAuthResult; /** * Internal bridge component. Use `ConvexProviderWithAuth` instead. * @internal */ declare function ConvexAuthBridge({ children }: { children: React.ReactNode; }): react_jsx_runtime0.JSX.Element; /** * Convex provider with auth bridge for @convex-dev/auth users. * Automatically wraps children with ConvexAuthBridge. * * @example * ```tsx * import { ConvexProviderWithAuth } from 'kitcn/react'; * * <ConvexProviderWithAuth client={convex} useAuth={useAuthFromConvexDev}> * <App /> * </ConvexProviderWithAuth> * ``` */ declare function ConvexProviderWithAuth$1({ children, ...props }: React.ComponentProps<typeof ConvexProviderWithAuth>): react_jsx_runtime0.JSX.Element; declare const useAuth: () => { hasSession: boolean; isAuthenticated: boolean; isLoading: boolean; }; /** Check if user maybe has auth (optimistic, has token) */ declare const useMaybeAuth: () => boolean; /** Check if user is authenticated (server-verified) */ declare const useIsAuth: () => boolean; declare const useAuthGuard: () => (callback?: () => Promise<void> | void) => boolean | undefined; /** Render children only when maybe has auth (optimistic) */ declare function MaybeAuthenticated({ children }: { children: React.ReactNode; }): react.ReactNode; /** Render children only when authenticated (server-verified) */ declare function Authenticated({ children }: { children: React.ReactNode; }): react.ReactNode; /** Render children only when maybe not auth (optimistic) */ declare function MaybeUnauthenticated({ children }: { children: React.ReactNode; }): react.ReactNode; /** Render children only when not authenticated (server-verified) */ declare function Unauthenticated({ children }: { children: React.ReactNode; }): react.ReactNode; //#endregion export { useMaybeAuth as C, useIsAuth as S, useAuthState as _, Authenticated as a, useConvexAuthBridge as b, FetchAccessTokenContext as c, MaybeUnauthenticated as d, Unauthenticated as f, useAuthGuard as g, useAuth as h, AuthStoreState as i, FetchAccessTokenFn as l, isSessionSyncGraceActive as m, AuthProvider as n, ConvexAuthBridge as o, decodeJwtExp as p, AuthStore as r, ConvexProviderWithAuth$1 as s, AUTH_SESSION_SYNC_GRACE_MS as t, MaybeAuthenticated as u, useAuthStore as v, useSafeConvexAuth as w, useFetchAccessToken as x, useAuthValue as y };