|
| 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 | +``` |
0 commit comments