UNPKG

kitcn

Version:

kitcn - React Query integration and CLI tools for Convex

174 lines (173 loc) 8.56 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 ConvexAuthRecoveryStatus = 'idle' | 'recovering' | 'failed'; type ConvexAuthRecoveryErrorCode = 'AUTH_PROVIDER_LOADING' | 'AUTH_PROVIDER_UNAUTHENTICATED' | 'AUTH_RECOVERY_CANCELLED' | 'AUTH_RECOVERY_FAILED' | 'AUTH_RECOVERY_TIMEOUT'; declare class ConvexAuthRecoveryError extends Error { readonly code: ConvexAuthRecoveryErrorCode; constructor(code: ConvexAuthRecoveryErrorCode, message: string); } type ConvexAuthRecoveryOptions = { /** Maximum time to wait for Convex backend confirmation. Defaults to 10s. */timeoutMs?: number; }; type ConvexAuthRecovery = { error: ConvexAuthRecoveryError | null; recover: (options?: ConvexAuthRecoveryOptions) => Promise<void>; status: ConvexAuthRecoveryStatus; }; /** Imperatively rebind Convex auth and wait for backend confirmation. */ declare function useConvexAuthRecovery(): ConvexAuthRecovery; type ConvexProviderWithAuthProps = React.ComponentProps<typeof ConvexProviderWithAuth>; 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, client, useAuth }: ConvexProviderWithAuthProps): 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 { useSafeConvexAuth as A, useAuthStore as C, useFetchAccessToken as D, useConvexAuthRecovery as E, useIsAuth as O, useAuthState as S, useConvexAuthBridge as T, Unauthenticated as _, Authenticated as a, useAuth as b, ConvexAuthRecoveryError as c, ConvexAuthRecoveryStatus as d, ConvexProviderWithAuth$1 as f, MaybeUnauthenticated as g, MaybeAuthenticated as h, AuthStoreState as i, useMaybeAuth as k, ConvexAuthRecoveryErrorCode as l, FetchAccessTokenFn as m, AuthProvider as n, ConvexAuthBridge as o, FetchAccessTokenContext as p, AuthStore as r, ConvexAuthRecovery as s, AUTH_SESSION_SYNC_GRACE_MS as t, ConvexAuthRecoveryOptions as u, decodeJwtExp as v, useAuthValue as w, useAuthGuard as x, isSessionSyncGraceActive as y };