Skip to content

Commit 05afc42

Browse files
authored
feat: add refetch function for all client hooks (#1206)
* feat: add refetch on client hooks * docs: add refetch docs * docs: add refetch docs
1 parent 1608444 commit 05afc42

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

docs/content/docs/basic-usage.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ Better Auth provides a `useSession` hook to easily access session data on the cl
230230
It has the following properties:
231231
- **data**: the actual session data which includes `session` and `user` object.
232232
- **isPending**: a boolean that indicates whether the session is being loaded.
233+
- **refetch**: a function that re-fetches the session and updates the value for all `useSession` usages.
233234
- **error**: an error object that contains any errors that occurred while loading the session.
234235

235236
<Tabs items={["React", "Vue","Svelte", "Solid", "Vanilla"]} defaultValue="React">
@@ -242,7 +243,8 @@ It has the following properties:
242243
const { // [!code highlight]
243244
data: session, // [!code highlight]
244245
isPending, //loading state // [!code highlight]
245-
error //error object // [!code highlight]
246+
error, //error object // [!code highlight]
247+
refetch //refetch the session
246248
} = authClient.useSession() // [!code highlight]
247249

248250
return (

docs/content/docs/concepts/client.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ On top of normal methods, the client provides hooks to easily access different r
101101
const {
102102
data: session,
103103
isPending, //loading state
104-
error //error object
104+
error, //error object
105+
refetch //refetch the session
105106
} = useSession()
106107
return (
107108
//...

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ export const useAuthQuery = <T>(
2424
error: null | BetterFetchError;
2525
isPending: boolean;
2626
isRefetching: boolean;
27+
refetch: () => void;
2728
}>({
2829
data: null,
2930
error: null,
3031
isPending: true,
3132
isRefetching: false,
33+
refetch: () => {
34+
return fn();
35+
},
3236
});
3337

3438
const fn = () => {
@@ -51,6 +55,7 @@ export const useAuthQuery = <T>(
5155
error: null,
5256
isPending: false,
5357
isRefetching: false,
58+
refetch: value.value.refetch,
5459
});
5560
}
5661
await opts?.onSuccess?.(context);
@@ -68,6 +73,7 @@ export const useAuthQuery = <T>(
6873
data: null,
6974
isPending: false,
7075
isRefetching: false,
76+
refetch: value.value.refetch,
7177
});
7278
await opts?.onError?.(context);
7379
},
@@ -78,6 +84,7 @@ export const useAuthQuery = <T>(
7884
data: currentValue.data,
7985
error: null,
8086
isRefetching: true,
87+
refetch: value.value.refetch,
8188
});
8289
await opts?.onRequest?.(context);
8390
},

packages/better-auth/src/client/react/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export function createAuthClient<Option extends ClientOptions>(
8787
data: Session;
8888
isPending: boolean;
8989
error: BetterFetchError | null;
90+
refetch: () => void;
9091
};
9192
$Infer: {
9293
Session: NonNullable<Session>;

0 commit comments

Comments
 (0)