npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

better-auth-react-query

v0.0.5

Published

A TanStack React Query client for Better Auth.

Downloads

634

Readme

better-auth-react-query

A TanStack React Query client wrapper for Better Auth. This package transforms your Better Auth client into a TanStack Query-compatible client with full TypeScript support.

Installation

npm install better-auth-react-query
# or
yarn add better-auth-react-query
# or
pnpm add better-auth-react-query

Requirements

  • @tanstack/react-query >= 5.0.0
  • better-auth >= 1.4.0

Usage

Setup

First, create a query client from your Better Auth client:

import { createAuthQueryClient } from 'better-auth-react-query'
import { createAuthClient } from 'better-auth/react'

const authClient = createAuthClient()
const auth = createAuthQueryClient(authClient)

Queries

Methods starting with get or list are automatically treated as queries. Use queryOptions() to get TanStack Query compatible options:

import { useQuery } from '@tanstack/react-query'

function Profile() {
  const { data: session } = useQuery(auth.getSession.queryOptions())

  return <div>Welcome, {session?.user.name}</div>
}

For queries that require input parameters:

const { data: user } = useQuery(
  auth.admin.getUser.queryOptions({ query: { id: '123' } }),
)

Passing query options:

const { data: user } = useQuery(
  auth.admin.getUser.queryOptions(
    { query: { id: '123' } },
    {
      staleTime: 100,
    },
  ),
)

Query Keys

You can access query keys directly for cache invalidation:

import { useQueryClient } from '@tanstack/react-query'

const queryClient = useQueryClient()

// Invalidate the session query
queryClient.invalidateQueries({ queryKey: auth.getSession.queryKey() })

// With parameters
queryClient.invalidateQueries({
  queryKey: auth.admin.getUser.queryKey({ query: { id: : '123'} }),
})

Mutations

All other methods are treated as mutations. Use mutationOptions() to get TanStack Query compatible options:

import { useMutation } from '@tanstack/react-query'

function SignInForm() {
  const signIn = useMutation(auth.signIn.email.mutationOptions())

  const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault()
    const formData = new FormData(e.currentTarget)

    signIn.mutate({
      email: formData.get('email') as string,
      password: formData.get('password') as string,
    })
  }

  return (
    <form onSubmit={handleSubmit}>
      <input name="email" type="email" />
      <input name="password" type="password" />
      <button type="submit" disabled={signIn.isPending}>
        Sign In
      </button>
    </form>
  )
}

Error Handling

Errors from Better Auth are automatically thrown, making them compatible with TanStack Query's error handling:

const signIn = useMutation(
  auth.signIn.email.mutationOptions({
    onError: (error) => {
      console.error('Sign in failed:', error.message)
    },
  }),
)

TypeScript

The package provides full type inference. All query and mutation options are properly typed based on your Better Auth client configuration:

// Types are inferred from your auth client
const { data } = useQuery(auth.getSession.queryOptions())
// data is typed as your session type

const signUp = useMutation(auth.signUp.email.mutationOptions())
// signUp.mutate() expects the correct input type

License

Apache-2.0