Local install gives you full control over your Better Auth schema, allows schema-related configuration to work, and makes it possible to use plugins beyond those supported out of the box. It also allows you to write Convex functions that directly access Better Auth component tables. With this approach, the Better Auth plugin is defined in its own Convex subdirectory. Installation differs from the default approach and includes a schema generation step via the Better Auth CLI, similar to the installation experience with other providers.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/get-convex/better-auth/llms.txt
Use this file to discover all available pages before exploring further.
Installation
Before you begin, follow the Getting Started guide to set up
Convex + Better Auth for your project. Then return here to convert the default
install to a local install.
Create the component definition
Create a
convex/betterAuth/convex.config.ts file to define the component. This signals to Convex that the convex/betterAuth directory is a locally installed component.convex/betterAuth/convex.config.ts
Generate the schema
Add a static Then generate the schema for the component.
auth export to a convex/betterAuth/auth.ts file.convex/betterAuth/auth.ts
Split out createAuthOptions function
Code in your component directory needs access to your Better Auth options, but running
createAuth() inside the component directory will trigger errors from Better Auth due to lack of environment variable access.To avoid this, create a separate createAuthOptions function that returns only the typed options object, and update createAuth to call it.convex/auth.ts
Update component registration
Update
convex/convex.config.ts to import the component definition from the local directory instead of the package.convex/convex.config.ts
Usage
Updating the schema
Certain option changes may require schema regeneration. The Better Auth docs will often note when this is the case. To regenerate the schema at any time, move into the component directory and run the Better Auth CLI generate command.Adding custom indexes
Some database interactions through Better Auth may run queries that don’t use an index. The Better Auth component automatically selects a suitable index for a given query if one exists, and will log a warning indicating what index should be added. Custom indexes can be added by generating the schema to a secondary file, importing it intoconvex/betterAuth/schema.ts, and adding the indexes. This way custom indexes aren’t overwritten when the schema is regenerated.
Schema table names and fields should not be customized directly, as any
customizations won’t match your Better Auth configuration and will be
overwritten when the schema is regenerated. Instead, customize the Better Auth
schema through options.
Accessing component data
Convex functions within your Better Auth component directory can access the component’s tables directly, and can then be called from outside the component viactx.runQuery, ctx.runMutation, or ctx.runAction.
Note that internal functions defined in a component are not accessible from outside it, so functions that need to run from outside the component must be public. While public functions are normally exposed to the internet, Convex functions exported by a component are never exposed to the internet, even if they are public.
If a function in a component is called from outside the component, the return
type won’t be inferred unless a return validator is provided.
convex/betterAuth/someFile.ts
convex/someFile.ts