Skip to content

Commit 4dd175e

Browse files
committed
chore: add better auth tanstack query client docs
1 parent 2f5f4b1 commit 4dd175e

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed

apps/website/app/(saas-js)/docs/header.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ const HeaderDesktopNavbar = () => {
237237
</PrimaryNavLink>
238238
}
239239
>
240+
<Menu.Item value="better-auth-react-query" asChild>
241+
<Link href="/docs/better-auth-react-query">
242+
Better Auth React Query
243+
</Link>
244+
</Menu.Item>
240245
<Menu.Item value="drizzle-crud" asChild>
241246
<Link href="/docs/drizzle-crud">Drizzle CRUD</Link>
242247
</Menu.Item>

apps/website/app/(saas-js)/docs/page.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ export default function Page() {
5757
</Heading>
5858

5959
<List.Root textStyle="lg" gap="2" listStyle="none">
60+
<List.Item>
61+
<Link
62+
href="/docs/better-auth-react-query"
63+
colorPalette="cyan"
64+
fontWeight="medium"
65+
>
66+
Better Auth React Query
67+
</Link>
68+
</List.Item>
6069
<List.Item>
6170
<Link
6271
href="/docs/drizzle-crud"
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
title: Better Auth React Query
3+
description: A TanStack Query client wrapper for Better Auth.
4+
---
5+
6+
A TanStack Query client wrapper for [Better Auth](https://better-auth.com). This
7+
package transforms your Better Auth client into a TanStack Query-compatible
8+
client with full TypeScript support.
9+
10+
## Installation
11+
12+
```bash
13+
npm install better-auth-react-query
14+
# or
15+
yarn add better-auth-react-query
16+
# or
17+
pnpm add better-auth-react-query
18+
```
19+
20+
## Requirements
21+
22+
- `@tanstack/react-query` >= 5.0.0
23+
- `better-auth` >= 1.4.0
24+
25+
## Usage
26+
27+
### Setup
28+
29+
First, create a query client from your Better Auth client:
30+
31+
```ts
32+
import { createAuthQueryClient } from 'better-auth-react-query'
33+
import { createAuthClient } from 'better-auth/react'
34+
35+
const authClient = createAuthClient()
36+
const auth = createAuthQueryClient(authClient)
37+
```
38+
39+
### Queries
40+
41+
Methods starting with `get` or `list` are automatically treated as queries. Use
42+
`queryOptions()` to get TanStack Query compatible options:
43+
44+
```tsx
45+
import { useQuery } from '@tanstack/react-query'
46+
47+
function Profile() {
48+
const { data: session } = useQuery(auth.getSession.queryOptions())
49+
50+
return <div>Welcome, {session?.user.name}</div>
51+
}
52+
```
53+
54+
For queries that require input parameters:
55+
56+
```tsx
57+
const { data: user } = useQuery(
58+
auth.admin.getUser.queryOptions({ query: { id: '123' } }),
59+
)
60+
```
61+
62+
Passing query options:
63+
64+
```tsx
65+
const { data: user } = useQuery(
66+
auth.admin.getUser.queryOptions(
67+
{ query: { id: '123' } },
68+
{
69+
staleTime: 100,
70+
},
71+
),
72+
)
73+
```
74+
75+
### Query Keys
76+
77+
You can access query keys directly for cache invalidation:
78+
79+
```ts
80+
import { useQueryClient } from '@tanstack/react-query'
81+
82+
const queryClient = useQueryClient()
83+
84+
// Invalidate the session query
85+
queryClient.invalidateQueries({ queryKey: auth.getSession.queryKey() })
86+
87+
// With parameters
88+
queryClient.invalidateQueries({
89+
queryKey: auth.admin.getUser.queryKey({ query: { id: : '123'} }),
90+
})
91+
```
92+
93+
### Mutations
94+
95+
All other methods are treated as mutations. Use `mutationOptions()` to get
96+
TanStack Query compatible options:
97+
98+
```tsx
99+
import { useMutation } from '@tanstack/react-query'
100+
101+
function SignInForm() {
102+
const signIn = useMutation(auth.signIn.email.mutationOptions())
103+
104+
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
105+
e.preventDefault()
106+
const formData = new FormData(e.currentTarget)
107+
108+
signIn.mutate({
109+
email: formData.get('email') as string,
110+
password: formData.get('password') as string,
111+
})
112+
}
113+
114+
return (
115+
<form onSubmit={handleSubmit}>
116+
<input name="email" type="email" />
117+
<input name="password" type="password" />
118+
<button type="submit" disabled={signIn.isPending}>
119+
Sign In
120+
</button>
121+
</form>
122+
)
123+
}
124+
```
125+
126+
### Error Handling
127+
128+
Errors from Better Auth are automatically thrown, making them compatible with
129+
TanStack Query's error handling:
130+
131+
```tsx
132+
const signIn = useMutation(
133+
auth.signIn.email.mutationOptions({
134+
onError: (error) => {
135+
console.error('Sign in failed:', error.message)
136+
},
137+
}),
138+
)
139+
```
140+
141+
## TypeScript
142+
143+
The package provides full type inference. All query and mutation options are
144+
properly typed based on your Better Auth client configuration:
145+
146+
```ts
147+
// Types are inferred from your auth client
148+
const { data } = useQuery(auth.getSession.queryOptions())
149+
// data is typed as your session type
150+
151+
const signUp = useMutation(auth.signUp.email.mutationOptions())
152+
// signUp.mutate() expects the correct input type
153+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"title": "Better Auth React Query",
3+
"root": true,
4+
"pages": ["index"]
5+
}

0 commit comments

Comments
 (0)