Skip to main content

BetterAuth

The BetterAuth type represents a configured Better Auth instance with full type safety based on the provided options.

Type Definition

import { betterAuth, BetterAuthOptions } from 'better-auth';

export type BetterAuth<TOptions extends BetterAuthOptions> = ReturnType<
typeof betterAuth<TOptions>
>;

Description

BetterAuth<TOptions> is a generic type that wraps the return type of the Better Auth library's betterAuth function. It preserves the type information from your Better Auth configuration, ensuring that all authentication features and plugins are properly typed throughout your application.

Type Parameters

  • TOptions - Extends BetterAuthOptions from the Better Auth library. This parameter captures your specific Better Auth configuration, including enabled authentication methods, plugins, and database settings.

Usage

Basic Usage

const options = {
database: new BetterSqlite3('./auth.db'),
emailAndPassword: {
enabled: true,
},
} as const satisfies BetterAuthOptions;

// Type-safe Better Auth instance
const auth: BetterAuth<typeof options> = betterAuth(options);

With Container Modules

The BetterAuth type is commonly used with container modules to ensure type safety when setting up authentication in your dependency injection container:

const options = {
database: new BetterSqlite3('./auth.db'),
emailAndPassword: {
enabled: true,
},
} as const satisfies BetterAuthOptions;

const container: Container = new Container();

const betterAuthInstance: BetterAuth<typeof options> = betterAuth(options);

const containerModule: BetterAuthExpressContainerModule<
typeof options,
() => BetterAuth<typeof options>
> = BetterAuthExpressContainerModule.fromOptions(
'/api/auth',
betterAuthInstance,
);

await container.load(containerModule);

Type Safety Benefits

  • Configuration Inference: The type preserves your exact Better Auth configuration
  • Method Availability: Ensures only configured authentication methods are available
  • Database Schema: Reflects your database schema and user/session types