Skip to content

Commit 73da454

Browse files
authored
fix(client): avoid atom to be proxy (#4079)
1 parent bb634a0 commit 73da454

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

packages/better-auth/src/client/client.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createAuthClient as createSolidClient } from "./solid";
44
import { createAuthClient as createReactClient } from "./react";
55
import { createAuthClient as createVueClient } from "./vue";
66
import { createAuthClient as createSvelteClient } from "./svelte";
7+
import { createAuthClient as createVanillaClient } from "./vanilla";
78
import { testClientPlugin, testClientPlugin2 } from "./test-plugin";
89
import type { Accessor } from "solid-js";
910
import type { Ref } from "vue";
@@ -12,8 +13,15 @@ import type { Session } from "../types";
1213
import { BetterFetchError } from "@better-fetch/fetch";
1314
import { twoFactorClient } from "../plugins";
1415
import { organizationClient, passkeyClient } from "./plugins";
16+
import { isProxy } from "node:util/types";
1517

1618
describe("run time proxy", async () => {
19+
it("atom in proxy should not be proxy", async () => {
20+
const client = createVanillaClient();
21+
const atom = client.$store.atoms.session;
22+
expect(isProxy(atom)).toBe(false);
23+
});
24+
1725
it("proxy api should be called", async () => {
1826
let apiCalled = false;
1927
const client = createSolidClient({

packages/better-auth/src/client/proxy.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { BetterFetch, BetterFetchOption } from "@better-fetch/fetch";
22
import type { Atom, PreinitializedWritableAtom } from "nanostores";
33
import type { ProxyRequest } from "./path-to-object";
44
import type { BetterAuthClientPlugin } from "./types";
5+
import { isAtom } from "../utils/is-atom";
56

67
function getMethod(
78
path: string,
@@ -55,6 +56,9 @@ export function createDynamicPathProxy<T extends Record<string, any>>(
5556
if (typeof current === "function") {
5657
return current;
5758
}
59+
if (isAtom(current)) {
60+
return current;
61+
}
5862
return createProxy(fullPath);
5963
},
6064
apply: async (_, __, args) => {

packages/better-auth/src/test-utils/test-instance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from "fs/promises";
22
import { generateRandomString } from "../crypto/random";
3-
import { afterAll, onTestFinished } from "vitest";
3+
import { afterAll } from "vitest";
44
import { betterAuth } from "../auth";
55
import { createAuthClient } from "../client/vanilla";
66
import type { BetterAuthOptions, ClientOptions, Session, User } from "../types";
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { Atom } from "nanostores";
2+
3+
export function isAtom(value: unknown): value is Atom<unknown> {
4+
return (
5+
typeof value === "object" &&
6+
value !== null &&
7+
"get" in value &&
8+
typeof (value as any).get === "function" &&
9+
"lc" in value &&
10+
typeof (value as any).lc === "number"
11+
);
12+
}

0 commit comments

Comments
 (0)