Skip to main content

Introduction

Better Auth is a comprehensive authentication library for TypeScript that provides secure, type-safe authentication with excellent developer experience. The InversifyJS HTTP framework provides seamless integration with Better Auth, allowing you to easily add authentication to your applications across Express, Express 4, Fastify, and Hono.

Features

  • Type-safe authentication: Full TypeScript support with type inference
  • Multiple adapters: Support for Express, Express 4, Fastify, and Hono
  • Session management: Built-in session handling with middleware integration
  • Flexible configuration: Easy setup with various authentication providers
  • Parameter decorators: Convenient access to user sessions in your controllers

Quick Start

The integration provides container modules that automatically set up Better Auth endpoints and middleware for each supported framework.

Start by installing the necessary packages:

npm install @inversifyjs/http-better-auth better-auth

Then, choose the appropriate container module for your framework:

const container: Container = new Container();

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

const betterAuthInstance = betterAuth(options);

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

await container.load(betterAuthExpressContainerModule);

const adapter: InversifyExpressHttpAdapter = new InversifyExpressHttpAdapter(
container,
{
logger: true,
useCookies: true,
},
);

const application: express.Application = await adapter.build();

application.listen();

Refer to the Better Auth Documentation for detailed setup instructions and advanced configuration options.