@better-auth-ui/core
Version:
Authentication components and data utilities for [Better Auth](https://better-auth.com), available for React and Solid.
71 lines (70 loc) • 4.53 kB
TypeScript
import { QueryClient, QueryOptions, skipToken } from '@tanstack/query-core';
import { AuthClient, InferData } from '../lib/auth-client';
export type ListSessionsData<TAuthClient extends AuthClient = AuthClient> = InferData<TAuthClient["listSessions"]>;
export type ListSessionsParams<TAuthClient extends AuthClient> = Parameters<TAuthClient["listSessions"]>[0];
export type ListSessionsOptions<TAuthClient extends AuthClient> = Partial<Omit<QueryOptions<ListSessionsData<TAuthClient>>, "queryKey">> & ListSessionsParams<TAuthClient>;
export type ListSession<TAuthClient extends AuthClient = AuthClient> = NonNullable<ListSessionsData<TAuthClient>>[number];
/**
* Query options factory for the current user's active sessions.
*
* @param authClient - The Better Auth client.
* @param userId - The current signed-in user's ID. Used for cache partitioning.
* @param params - Parameters forwarded to `authClient.listSessions`.
*/
export declare function listSessionsOptions<TAuthClient extends AuthClient>(authClient: TAuthClient, userId?: string, params?: ListSessionsParams<TAuthClient>): {
queryKey: readonly ["auth", "user", string | undefined, "listSessions", Record<string, any> | null];
queryFn: typeof skipToken | (({ signal }: {
client: QueryClient;
queryKey: readonly unknown[];
signal: AbortSignal;
meta: import('@tanstack/query-core').QueryMeta | undefined;
pageParam?: unknown;
direction?: unknown;
}) => Promise<InferData<TAuthClient["listSessions"]>>);
};
/**
* Get the current user's active sessions from the query cache, calling
* `fetchListSessions` under the hood if no cached entry exists. Resolves with
* the sessions data, making it ideal for loaders or `beforeLoad` guards.
*
* @param queryClient - The TanStack Query client.
* @param authClient - The Better Auth client.
* @param userId - The current signed-in user's ID. Used for cache partitioning.
* @param options - Params forwarded to `authClient.listSessions` and query options.
*/
export declare const ensureListSessions: <TAuthClient extends AuthClient>(queryClient: QueryClient, authClient: TAuthClient, userId: string, options?: ListSessionsOptions<TAuthClient>) => Promise<InferData<TAuthClient["listSessions"]>>;
/**
* Prefetch the current user's active sessions into the query cache. Behaves
* like `fetchListSessions`, but does not throw on error and does not return
* the data — use this to warm the cache without blocking navigation.
*
* @param queryClient - The TanStack Query client.
* @param authClient - The Better Auth client.
* @param userId - The current signed-in user's ID. Used for cache partitioning.
* @param options - Params forwarded to `authClient.listSessions` and query options.
*/
export declare const prefetchListSessions: <TAuthClient extends AuthClient>(queryClient: QueryClient, authClient: TAuthClient, userId: string, options?: ListSessionsOptions<TAuthClient>) => Promise<void>;
/**
* Fetch and cache the current user's active sessions, resolving with the data
* or throwing on error. If a cached entry exists and is neither invalidated
* nor older than `staleTime`, the cached value is returned without a network
* call; otherwise the latest data is fetched.
*
* @param queryClient - The TanStack Query client.
* @param authClient - The Better Auth client.
* @param userId - The current signed-in user's ID. Used for cache partitioning.
* @param options - Params forwarded to `authClient.listSessions` and query options.
*/
export declare const fetchListSessions: <TAuthClient extends AuthClient>(queryClient: QueryClient, authClient: TAuthClient, userId: string, options?: ListSessionsOptions<TAuthClient>) => Promise<InferData<TAuthClient["listSessions"]>>;
/**
* Read the current user's active sessions synchronously from the query cache
* without triggering a fetch. Returns the cached sessions data, or `undefined`
* when no entry exists — use this for non-suspending reads where a network
* call is undesirable.
*
* @param queryClient - The TanStack Query client.
* @param authClient - The Better Auth client.
* @param userId - The current signed-in user's ID. Used for cache partitioning.
* @param params - Params forwarded to `authClient.listSessions`.
*/
export declare const getListSessions: <TAuthClient extends AuthClient = AuthClient>(queryClient: QueryClient, _authClient?: TAuthClient, userId?: string, params?: ListSessionsParams<TAuthClient>) => InferData<TAuthClient["listSessions"]> | undefined;