The component client is returned fromDocumentation 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.
createClient() and provides methods for using Better Auth inside your Convex functions.
createClient(component, options?)
Factory function that creates the component client. Call it once in convex/auth.ts and export the result.
convex/auth.ts
Type parameter
Your Convex data model, imported from
./_generated/dataModel.Parameters
The
components.betterAuth reference from ./_generated/api, registered in convex.config.ts.Optional configuration.
adapter(ctx)
Returns the Convex database adapter for use in betterAuth({ database: ... }).
convex/auth.ts
Parameters
The Convex context passed into the enclosing function (query, mutation, or action).
getAuth(createAuth, ctx)
Returns { auth, headers } for calling auth.api methods directly from a Convex mutation or query. Because Convex functions don’t have access to request headers, getAuth constructs a Headers object at runtime using the session token from the current user identity.
convex/users.ts
Parameters
Your
createAuth function exported from convex/auth.ts.The Convex context for the current function.
Returns
A promise resolving to{ auth, headers }:
auth— the Better Auth instance returned bycreateAuth(ctx).headers— aHeadersobject containing the current session token as aBearertoken. Pass this toauth.apimethods that require authentication.
getAuthUser(ctx)
Returns the current authenticated user after validating that the session has not expired. Throws a ConvexError("Unauthenticated") if no valid session is found.
convex/users.ts
Parameters
The Convex context for the current function.
Returns
A promise resolving to the current user document, or throwsConvexError("Unauthenticated") if there is no authenticated user or the session has expired.
If you want a nullable return instead of throwing, use
safeGetAuthUser(ctx),
which returns undefined when no authenticated user is found.getAnyUserById(ctx, id)
Returns a user by their Better Auth user ID, regardless of the current session.
convex/users.ts
Parameters
The Convex context for the current function.
The Better Auth user ID (the
_id field on the user table).Returns
A promise resolving to the user document, ornull if no user with that ID exists.
registerRoutes(http, createAuth, options?)
Registers Better Auth HTTP handlers on a Convex HttpRouter. Use this in convex/http.ts.
convex/http.ts
Parameters
The Convex HTTP router instance.
Your
createAuth function. It is called immediately during route registration to read basePath from the Better Auth options.registerRoutes calls createAuth({}) at registration time to read basePath, which initializes Better Auth eagerly. Use registerRoutesLazy to defer this and reduce memory usage during deploy.registerRoutesLazy(http, createAuth, options?)
Lazy version of registerRoutes that defers Better Auth initialization until the first request. This avoids loading Better Auth at deploy time, which can prevent out-of-memory errors in convex/http.ts.
convex/http.ts
basePath explicitly if you use a non-default path. If using CORS, pass trustedOrigins directly so the OPTIONS pre-flight handler can respond without loading Better Auth.
Parameters
The Convex HTTP router instance.
Your
createAuth function. Not called until the first auth request arrives.triggersApi()
Returns internal mutations (onCreate, onUpdate, onDelete) that execute the trigger callbacks defined in createClient options. Export these from convex/auth.ts so the component can call them.
convex/auth.ts
Returns
An object with three internal mutations:onCreate— called after a document is created in a Better Auth table.onUpdate— called after a document is updated.onDelete— called after a document is deleted.
{ model, doc } (or { model, newDoc, oldDoc } for updates) and dispatches to the matching callback in your triggers config.
clientApi()
Returns a getAuthUser Convex query for use with the AuthBoundary component. Export the result from convex/auth.ts.
convex/auth.ts
AuthBoundary:
components/ClientAuthBoundary.tsx
Returns
An object containing:getAuthUser— a Convex query that returns the current user or throwsConvexError("Unauthenticated")if there is no valid session.