From b5f119f6c894ce5698da88aa5be8897c0a4076b2 Mon Sep 17 00:00:00 2001 From: Nico Audy Date: Thu, 31 Oct 2024 15:16:49 +0700 Subject: [PATCH 1/4] docs: fix hono integration doc Wrap response to object instead, the second params should is status code --- docs/content/docs/integrations/hono.mdx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/content/docs/integrations/hono.mdx b/docs/content/docs/integrations/hono.mdx index ceeeb6d5f6..0837bac306 100644 --- a/docs/content/docs/integrations/hono.mdx +++ b/docs/content/docs/integrations/hono.mdx @@ -101,6 +101,9 @@ app.get("/session", async (c) => { if(!user) return c.body(null, 401); - return c.json(session, user); + return c.json({ + session, + user + }); }); ``` From b822836954402c81ba818f066f3807c1cc6f3640 Mon Sep 17 00:00:00 2001 From: Bereket Engida <86073083+Bekacru@users.noreply.github.com> Date: Thu, 31 Oct 2024 13:00:01 +0300 Subject: [PATCH 2/4] feat: expose client store (#365) --- examples/expo-example/expo-env.d.ts | 3 + packages/better-auth/src/client/config.ts | 57 +++++++++++++------ packages/better-auth/src/client/query.ts | 1 + packages/better-auth/src/client/react.ts | 36 +++++++----- .../better-auth/src/client/session-atom.ts | 10 +--- packages/better-auth/src/client/solid.ts | 29 ++++++---- packages/better-auth/src/client/svelte.ts | 33 +++++++---- .../better-auth/src/client/test-plugin.ts | 28 ++++----- packages/better-auth/src/client/types.ts | 16 ++++-- packages/better-auth/src/client/vanilla.ts | 23 +++++--- packages/better-auth/src/client/vue.ts | 40 ++++++++----- .../src/plugins/multi-session/client.ts | 2 +- .../src/plugins/organization/client.ts | 16 +++--- .../src/plugins/phone-number/client.ts | 2 +- .../src/plugins/two-factor/client.ts | 2 +- 15 files changed, 182 insertions(+), 116 deletions(-) create mode 100644 examples/expo-example/expo-env.d.ts diff --git a/examples/expo-example/expo-env.d.ts b/examples/expo-example/expo-env.d.ts new file mode 100644 index 0000000000..bf3c1693a0 --- /dev/null +++ b/examples/expo-example/expo-env.d.ts @@ -0,0 +1,3 @@ +/// + +// NOTE: This file should not be edited and should be in your git ignore diff --git a/packages/better-auth/src/client/config.ts b/packages/better-auth/src/client/config.ts index de75f96a42..bc92c691c8 100644 --- a/packages/better-auth/src/client/config.ts +++ b/packages/better-auth/src/client/config.ts @@ -1,45 +1,46 @@ import { createFetch } from "@better-fetch/fetch"; import { getBaseURL } from "../utils/url"; -import { type Atom } from "nanostores"; +import { type Atom, type WritableAtom } from "nanostores"; import type { AtomListener, ClientOptions } from "./types"; import { addCurrentURL, redirectPlugin } from "./fetch-plugins"; +import { getSessionAtom } from "./session-atom"; export const getClientConfig = (options?: O) => { /* check if the credentials property is supported. Useful for cf workers */ const isCredentialsSupported = "credentials" in Request.prototype; - const baseURL = getBaseURL( - options?.fetchOptions?.baseURL || options?.baseURL, - ); + const baseURL = getBaseURL(options?.baseURL); + const pluginsFetchPlugins = + options?.plugins + ?.flatMap((plugin) => plugin.fetchPlugins) + .filter((pl) => pl !== undefined) || []; const $fetch = createFetch({ baseURL, ...(isCredentialsSupported ? { credentials: "include" } : {}), method: "GET", ...options?.fetchOptions, - plugins: options?.disableDefaultFetchPlugins - ? options.fetchOptions?.plugins + ? [...(options?.fetchOptions?.plugins || []), ...pluginsFetchPlugins] : [ redirectPlugin, addCurrentURL, - ...(options?.fetchOptions?.plugins?.filter( - (pl) => pl !== undefined, - ) || []), - ...(options?.plugins - ?.flatMap((plugin) => plugin.fetchPlugins) - .filter((pl) => pl !== undefined) || []), + ...(options?.fetchOptions?.plugins || []), + ...pluginsFetchPlugins, ], }); - + const { $sessionSignal, session } = getSessionAtom($fetch); const plugins = options?.plugins || []; let pluginsActions = {} as Record; - let pluginsAtoms = {} as Record>; + let pluginsAtoms = { + $sessionSignal, + session, + } as Record>; let pluginPathMethods: Record = { "/sign-out": "POST", "/revoke-sessions": "POST", }; const atomListeners: AtomListener[] = [ { - signal: "_sessionSignal", + signal: "$sessionSignal", matcher(path) { return ( path === "/sign-out" || @@ -50,10 +51,8 @@ export const getClientConfig = (options?: O) => { }, }, ]; + for (const plugin of plugins) { - if (plugin.getActions) { - Object.assign(pluginsActions, plugin.getActions?.($fetch)); - } if (plugin.getAtoms) { Object.assign(pluginsAtoms, plugin.getAtoms?.($fetch)); } @@ -64,11 +63,33 @@ export const getClientConfig = (options?: O) => { atomListeners.push(...plugin.atomListeners); } } + + const $store = { + notify: (signal?: Omit | "$sessionSignal") => { + pluginsAtoms[signal as keyof typeof pluginsAtoms].set( + !pluginsAtoms[signal as keyof typeof pluginsAtoms].get(), + ); + }, + listen: ( + signal: Omit | "$sessionSignal", + listener: (value: boolean, oldValue?: boolean | undefined) => void, + ) => { + pluginsAtoms[signal as keyof typeof pluginsAtoms].subscribe(listener); + }, + atoms: pluginsAtoms, + }; + + for (const plugin of plugins) { + if (plugin.getActions) { + Object.assign(pluginsActions, plugin.getActions?.($fetch, $store)); + } + } return { pluginsActions, pluginsAtoms, pluginPathMethods, atomListeners, $fetch, + $store, }; }; diff --git a/packages/better-auth/src/client/query.ts b/packages/better-auth/src/client/query.ts index 7833723cc4..3dc8ba70c7 100644 --- a/packages/better-auth/src/client/query.ts +++ b/packages/better-auth/src/client/query.ts @@ -40,6 +40,7 @@ export const useAuthQuery = ( isPending: value.get().isPending, }) : options; + return $fetch(path, { ...opts, onSuccess: async (context) => { diff --git a/packages/better-auth/src/client/react.ts b/packages/better-auth/src/client/react.ts index 9da84da2b5..19f3fbf08a 100644 --- a/packages/better-auth/src/client/react.ts +++ b/packages/better-auth/src/client/react.ts @@ -6,11 +6,13 @@ import type { ClientOptions, InferActions, InferClientAPI, + InferSessionFromClient, + InferUserFromClient, IsSignal, } from "./types"; import { createDynamicPathProxy } from "./proxy"; -import { getSessionAtom } from "./session-atom"; import type { UnionToIntersection } from "../types/helper"; +import type { BetterFetchError } from "@better-fetch/fetch"; function getAtomKey(str: string) { return `use${capitalizeFirstLetter(str)}`; @@ -46,40 +48,46 @@ export function createAuthClient